javascript - MongoDB Stitch Functions 是否支持 async/await 定义?
问题描述
MongoDB (Atlas) Stitch 上的异步函数定义在 GUI 编辑器上显示警告。包括触发器参考中提供的示例代码。
此处找到的代码可以直接复制到 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 () => {})
将提示可以解决的错误。
运行时执行返回标准异步操作的预期结果。
推荐阅读
- node.js - node.js 加密中的长度密钥或 iv 无效
- java - 当我自定义高度和自定义背景时,为什么按钮文本不是 wrap_content 并且消失了?
- android - android中的多个通知声音
- javascript - 外容器上的溢出属性导致负边距不起作用
- tensorflow - 如何将可训练权重添加到自定义 Keras 层?
- c - 为什么即使不导入编写它们的库,C 函数也能工作?
- reactjs - 如何使用 i18next 在同一个反应应用程序上使用两个翻译?
- android - expo 上的动画节点错误仅发生在 android 手机上
- python - 散景悬停工具显示过于拥挤,无法显示多条图表线
- javascript - 如何将用户从任何 url 重定向到另一个?