I have a trigger on new documents in collection A, in which I want to get a related record from the users 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. 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");
}
2
Answers
console.log()
different variables in your script to see what value they got in run time?Without the keyword "trigger"
Try to use
findOne
instead offind
for the user.Replace:
with: