我正在将 GCP 与节点 16 和 firestore 结合使用。我正在获取子集合的数据并按 id 进行更新。在
console.log(subCollect.id);
和 console.log(subCollect.data());
中记录的信息是正确的。在更新命令中,我收到以下错误
var subCollecPromises = [];
var parentPromises = [];
var querySnapshot = await db.collectionGroup("mysubcollection")
.where('name', '==', "subname")
.where('sequence', '==', 2).get();
querySnapshot.forEach(doc => {
var data = doc.data();
if(data.enable){
var childRef;
var parentRef;
childRef = doc.ref.parent;
parentRef = childRef.parent;
subCollecPromises.push(doc.ref.get());
parentPromises.push(parentRef.get());
}
});
const arrsubCollecSnap = await Promise.all(subCollecPromises);
const arrsubParentSnap = await Promise.all(parentPromises);
for (let index = 0; index < arrsubParentSnap.length; index++) {
const item = arrsubParentSnap[index];
var parentData = item.data();
var subCollect = arrsubCollecSnap[index];
console.log(subCollect.id);
console.log(subCollect.data());
await db.collection("mysubcollection").doc(subCollect.id)
.update({sequence: 3, datetime: new Date()});
await sendMail(parentData.mail);
}
更新错误:
Error: 5 NOT_FOUND: no entity to update: app: "dev~myapp-backend"
path <
Element {
type: "mysubcollection"
name: "Vuxx2Hy9xprtm7tZFyne"
}
>
code: 5,
details: 'no entity to update: app: "dev~myapp-backend"\n' +
'path <\n' +
' Element {\n' +
' type: "mysubcollection"\n' +
' name: "Vuxx2Hy9xprtm7tZFyne"\n' +
' }\n' +
'>\n',
metadata: Metadata {
internalRepr: Map(1) { 'content-type' => [Array] },
options: {}
},
note: 'Exception occurred in retry method that was not classified as transient'
更新子集合的正确方法是从父集合开始:
await db.collection("parentCollection").doc(parentId).collection("mysubcollection").doc(subCollect.id).update({sequence: 3, datetime: new Date()});
我能够通过将我的数据库 ID 添加到 getFirestore() 作为 getFirestore(
示例:
await getFirestore(<database_id>).collection('users').doc(userRecord.uid).create({
email: email,
name: name,
phone: phone,
})