[英]MongoDB Atlas triggers - access different collection
I have a trigger on new documents in collection A, in which I want to get a related record from the users collections.我对集合 A 中的新文档有一个触发器,我想从中获取用户 collections 的相关记录。
I (think) I followed the documentation to the "t", but both the collection itself comes out empty and the actual user I'm trying to fetch.我(认为)我按照文档找到了“t”,但是集合本身都是空的,而且我正在尝试获取实际用户。 And ideas what I'm doing wrong?
以及我做错了什么的想法?
exports = async function trigger(changeEvent) {
const toolRecord = changeEvent.fullDocument;
const usersCollection = await context.services
.get("Cluster0")
.db("mind-tools")
.collection("users");
const user = await usersCollection.find({ _id: toolRecord.userId });
const users = await usersCollection.find({});
const text = [
"New tool record - " + toolRecord._id,
toolRecord.toolKey,
"users",
JSON.stringify(usersCollection),
JSON.stringify(users[0]),
"user",
toolRecord.userId,
JSON.stringify(user),
user.name,
user.email,
]
.filter(Boolean)
.join("\n");
}
try to use "findOne" instead of "find" for the user:尝试为用户使用“findOne”而不是“find”:
replace: const user = await usersCollection.find({ _id: toolRecord.userId });替换:const user = await usersCollection.find({ _id: toolRecord.userId });
to: const user = await usersCollection.findOne({ _id: toolRecord.userId });至:const user = await usersCollection.findOne({ _id: toolRecord.userId });
console.log()
different variables in your script to see what value they got in run time?console.log()
不同的变量来查看它们在运行时获得的值?exports = async function (changeEvent) { ...
Without the keyword "trigger"没有关键字“触发器”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.