简体   繁体   English

MongoDB Stitch Webhook中的“然后不是功能”

[英]'then is not a function' in MongoDB Stitch Webhook

The MongoDB Stitch Webhook docs describe my precise use case: using a POST method to call insertOne then return the inserted _id. MongoDB Stitch Webhook文档描述了我的确切用例:使用POST方法调用insertOne然后返回插入的_id。

I pasted the example below (directly from the docs) into the Stitch Function Editor. 我将下面的示例(直接从文档中)粘贴到了Stitch Function Editor中。

exports = function(payload, response) {
  const mongodb = context.services.get("mongodb-atlas");
  const requestLogs = mongodb.db("test").collection("requestlogs");
  requestLogs.insertOne({
    body: EJSON.parse(payload.body.text()),
    query: payload.query
  }).then(result => {
    response.setStatusCode(201);
    response.setBody(result.insertedId);
  })
};

I executed the function in the Function Editor console by calling: 我通过调用以下命令在函数编辑器控制台中执行了该函数:

exports({query: {arg1: 'hello', arg2: "world!"}, body:BSON.Binary.fromText('{"msg": "world"}')})

An error is returned indicating that .then is not a function. 返回一个错误,指示.then不是函数。

error: TypeError: 'then' is not a function 错误:TypeError: “然后”不是一个函数

Are the docs wrong, or I have I gone astray? 文档是错误的,还是我误入歧途?

Certain methods, like .then , throw errors in the function editor. 某些方法(如.then在函数编辑器中引发错误。 In my case, this was a shortcoming of the function editor, rather than an error in my code. 就我而言,这是函数编辑器的缺点,而不是代码中的错误。 Calling the webhook with fetch or Postman, the function executed as expected. fetch或Postman调用webhook,该函数按预期执行。

The Incoming Webhook docs contain a special note: 传入的Webhook文档包含一个特殊说明:

If you want to debug a webhook function response from the function editor, you must manually provide the HTTP response object when you run the function. 如果要从函数编辑器调试webhook函数响应,则在运行函数时必须手动提供HTTP响应对象。

exports( { body: "This document is the webhook payload" }, new HTTPResponse() ) exports({正文:“此文档是webhook负载”},新的HTTPResponse())

That alerted me to the idiosyncratic nature of the function editor as a JS handler. 这使我意识到函数编辑器作为JS处理程序的特质。 Using Postman I confirmed the function ran without errors when called. 使用邮递员,我确认调用该函数时没有错误。 The error generated by the function editor was a red herring. 函数编辑器生成的错误是红色鲱鱼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM