gpt4 book ai didi

javascript - Firebase Firestore : How to get ref from snapshot via using async/await

转载 作者:行者123 更新时间:2023-11-30 14:21:20 25 4
gpt4 key购买 nike

我在 Firebase 函数 中使用 Cloud FirestoreNode.js 8

简单的开放性问题是:是否可以通过使用 async/await.get() 获取 ref

示例:

const snapshot = await db.collection(/*..*/).doc(/*..*/).get();
const data = snapshot.data();
const ref = /* ???? */
// Then using...
ref.update({/*..*/});

还是我应该照做?

const ref = db.collection(/*..*/).doc(/*..*/);
const snapshot = await ref.get();
/* so on.../*

最佳答案

如果你想从你的快照常量中获取一个新的引用那么它是可能的我会这样示例

const areaSnapshot = await admin.firestore().doc("areas/greater-boston").get()
const bostonCities = areaSnapshot.data().cities;
const allAreas = await areaSnapshot.ref.parent.doc("new-york").get()
const nyCities= allAreas.data().cities
console.log(bostonCities, nyCities)

更新文档

    //to update document
const areaSnapshot = await admin.firestore().doc("areas/greater-boston").get()
const allAreas = areaSnapshot.ref.parent.doc("new-york").update({
capital: {
liberty: true
}
})
await allAreas
.then(() => {
console.log("success")
})
.catch(err => console.log(err))

来源: https://firebase.google.com/docs/firestore/manage-data/add-data

关于javascript - Firebase Firestore : How to get ref from snapshot via using async/await,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52698039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com