javascript - MongoDB Stitch Functions 是否支持异步/等待定义?

标签 javascript mongodb async-await mongodb-stitch

MongoDB (Atlas) 上的异步函数定义 Stitch 在 GUI 编辑器上显示警告。包括触发器引用中提供的示例代码。

代码找到here可以直接复制到 Stitch Function 编辑器,并由于 async 关键字而产生警告。

文档中的示例代码。

exports = async function (changeEvent) {
  // Destructure out fields from the change stream event object
  const { updateDescription, fullDocument } = changeEvent;

  // Check if the shippingLocation field was updated
  const updatedFields = Object.keys(updateDescription.updatedFields);
  const isNewLocation = updatedFields.some(field =>
    field.match(/shippingLocation/)
  );

  // If the location changed, text the customer the updated location.
  if (isNewLocation) {
    const { customerId, shippingLocation } = fullDocument;
    const twilio = context.services.get("myTwilioService");
    const mongodb = context.services.get("mongodb-atlas");
    const customers = mongodb.db("store").collection("customers");

    const { location } = shippingLocation.pop();
    const customer = await customers.findOne({ _id: customer_id })
    twilio.send({
      to: customer.phoneNumber,
      from: context.values.get("ourPhoneNumber"),
      body: `Your order has moved! The new location is ${location}.`
    });
  }
};

我想知道 Stitch 是否支持 async/await 范式,以及我是否应该关注显示的警告。

最佳答案

经过一些测试,我发现此时 async/await 关键字会导致 linter 抛出错误和警告。这意味着对于异步回调,最好单独定义它们,因为它会改进 linting。 IE。 [].map(async () => {}) 将提示可以解决的错误。

运行时执行返回标准异步操作的预期结果。

关于javascript - MongoDB Stitch Functions 是否支持异步/等待定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56237906/

相关文章:

javascript - 将 Intern 与 Selenium 和动态 proxyUrl 结合使用

javascript - 迭代时为 DOM 事件监听器分配变量

node.js - Mongoose - 通过 findOne() 搜索架构失败

java - mongodb + java + iso 日期搜索

浏览器取消请求时出现 ASP.NET Web API OperationCanceledException

javascript - 有人可以告诉我从字符串中删除特定内联 CSS 样式的最佳方法吗?

javascript - 如何解决这个 npm glob-parent 问题

javascript - Mongodb findAndModify Node js

c# - 不等待 SaveChangesAsync() 是否安全?

c# - NetworkStream ReadAsync 和 Read 同一个方法