Pusher - с функциями Wix HTTP
Я пытаюсь интегрировать Pusher с HTTP-функциями Wix
Например: на сайт Wix отправляется запрос GET (путь: '/findItems'). После того, как запрос сделан, я хочу проверить новые вставки элементов в базу данных. Я обнаружил, что это можно сделать с помощью хука afterInsert . Когда крючок зацепился , я хочу активировать толкатель.
Это код, который я сейчас использую
http-functions.js
:
import { ok, created, notFound, serverError } from 'wix-http-functions';
import wixData from 'wix-data';
import Pusher from "pusher";
const pusher = new Pusher({
appId: "xxxxx",
key: "xxxxxxxxxxxxxxx",
secret: "xxxxxxxxxxxx",
cluster: "xxxx",
useTLS: true
});
export function get_findItems(request) {
let options = {
"headers": {
"Content-Type": "application/json"
}
};
return wixData.query("users")
.eq("firstName", request.path[0])
.eq("lastName", request.path[1])
.find()
.then((results) => {
if (results.items.length > 0) {
options.body = {
"items": results.items
};
return ok(options);
}
options.body = {
"error": `'${request.path[0]} ${request.path[1]}' was not found`
};
return notFound(options);
})
.catch((error) => {
options.body = {
"error": error
};
return serverError(options);
});
}
export function post_newItem(request) {
let options = {
"headers": {
"Content-Type": "application/json"
}
};
return request.body.text()
.then( (body) => {
return wixData.insert("users", JSON.parse(body));
} )
.then( (results) => {
options.body = {
"inserted": results
};
return created(options);
} )
.catch( (error) => {
options.body = {
"error": error
};
return serverError(options);
} );
}
export function users_afterInsert(item, context) {
let hookContext = context;
pusher.trigger("channel", "action", {
firstName: item.firstName,
lastName: item.lastName
});
return item;
}
Но, к сожалению, Pusher не срабатывает. После отладки я обнаружил, что пакет Pusher установлен и работает, но не запускается только в хуке afterInsert!
Любая помощь приветствуется!
Спасибо !
1 ответ
Решение
Код для
afterInsert
ловушка должна быть в бэкэнд-файле с именем
data.js
, а не в
http-functions.js
файл в том виде, в котором он есть сейчас.