【问题标题】:Pusher - with Wix HTTP FunctionsPusher - 带有 Wix HTTP 功能
【发布时间】:2021-03-16 05:07:45
【问题描述】:

我正在尝试将 Pusher 与 Wix HTTP 函数集成

例如: 向 Wix 站点(路径:'/findItems')发出 GET 请求。发出请求后,我想检查数据库中是否有新插入的项目。这个,我发现,我可以用afterInsert 钩子来做。当钩子钩住时,我想触发 Pusher。
这是我目前使用的代码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 钩子中触发!
非常感谢任何帮助!

谢谢!

【问题讨论】:

  • 您的“wix”标签意味着 - Winows Installer Xml,而不是“免费网站构建器”
  • 糟糕!谢谢指正!

标签: pusher velo


【解决方案1】:

afterInsert 挂钩的代码需要在名为 data.js 的后端文件中,而不是像现在这样在 http-functions.js 文件中。

【讨论】:

    猜你喜欢
    • 2013-06-22
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2013-08-10
    • 2021-12-04
    • 1970-01-01
    相关资源
    最近更新 更多