在GUI编辑器上显示警告MongoDB(Atlas(上的异步函数定义。包括触发器的参考文献中提供的示例代码。
可以将此处找到的代码直接复制到缝合函数编辑器中,并由于异步关键字而发出警告。
文档中的示例代码
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}.`
});
}
};
我想知道针迹是否支持异步/等待范式,并且我是否应该担心所示的警告。
在进行了一些测试之后,我发现目前async/await
关键字会导致Linter发出错误和警告。这意味着,对于异步回调,最好将它们分开定义,因为它可以改善绵羊。IE。[].map(async () => {})
将提示可以解决的错误。
运行时执行将根据标准异步操作的预期返回结果。