英文:
Javascript parameter inside string not recognized
问题
以下是您要翻译的内容:
"For Firebase Cloudfunctions I am trying to update a value based on a dynmaic ID. This ID is saved inside a const value. The exact field I try to reach via dot-notation. Here is my complete function:
exports.badgeUpdated = functions.firestore .document("badges/{docId}") .onUpdate(async (change, context ) => { // get the document that has changed and the new fieldvalue const ObjectId = change.after.id; const newObject = change.after.data(); const newBadgeName = newObject.badgeName; // const newImageUrl = newObject.badgeImageUrl; console.log(`New BadgeName ${newBadgeName}`); console.log(`New BadgeID ${ObjectId}`); // Search badge doc with changed ID and set new name const querySnapshot = await admin .firestore() .collectionGroup("keekzCards") .where("badgeContains", "array-contains", ObjectId).get(); querySnapshot.forEach((doc) => { doc.ref.update({"badges.${ObjectId}.badgeName": newBadgeName}); console.log(doc.id); }); // console.log(`Queryresult ${(querySnapshot).docs}`); return null; });
The code itself is working, however instead of updating the field the function is creating an individual map-field using the plain string instead of resolving the underlying constant value. So after running the function my firestore document is looking like so:
EDIT: When exchaning the quotes the following error appears:
1: https://i.stack.imgur.com/nv7nd.png 2: https://i.stack.imgur.com/WYuVB.png"
英文:
For Firebase Cloudfunctions I am trying to update a value based on a dynmaic ID.
This ID is saved inside a const value. The exact field I try to reach via dot-notation.
Here is my complete function:
exports.badgeUpdated = functions.firestore
.document("badges/{docId}")
.onUpdate(async (change, context ) => {
// get the document that has changed and the new fieldvalue
const ObjectId = change.after.id;
const newObject = change.after.data();
const newBadgeName = newObject.badgeName;
// const newImageUrl = newObject.badgeImageUrl;
console.log(`New BadgeName ${newBadgeName}`);
console.log(`New BadgeID ${ObjectId}`);
// Search badge doc with changed ID and set new name
const querySnapshot = await admin
.firestore()
.collectionGroup("keekzCards")
.where("badgeContains", "array-contains", ObjectId).get();
querySnapshot.forEach((doc) => {
doc.ref.update({"badges.${ObjectId}.badgeName": newBadgeName});
console.log(doc.id);
});
// console.log(`Queryresult ${(querySnapshot).docs}`);
return null;
});
The code itself is working, however instead of updating the field the function is creating an individual map-field using the plain string instead of resolving the underlying constant value. So after running the function my firestore document is looking like so:
EDIT:
When exchaning the quotes the following error appears:
答案1
得分: 1
ObjectId
是一个保留关键字。避免使用它。改用objectId
或oId
代替:
const objectId = change.after.id;
英文:
ObjectId
is a reserved keyword. Avoid using it. Use instead objectId
or oId
:
const objectId = change.after.id;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论