[英]How to store ID of record in Firebase cloud functions
I'm saving data in the collection in the following way:我通过以下方式将数据保存在集合中:
const userEntry= {
UserId: "I want documentID here",
UserName: "",
creationDate: ""
}
const churchResult = await saveChurchData(userEntry)
const saveData = async (data: object) => {
return database.collection('users').add(data)
.then(snapshot => {
return snapshot.get().then(doc => {
doc.data()
return doc.id
}).catch(err => {
console.log('Error getting documents', err);
return null;
});
}).catch(err => {
console.log('Error getting documents', err);
return null;
});
}
Is there any way that I store "documentID" of users table in the place of UserId
.有什么方法可以将 users 表的“documentID”存储在UserId
的位置。 How can we do that in firebase cloud functions?我们如何在 firebase 云函数中做到这一点? I'm unable to find a way to store the documentID in the documentation.我无法找到将 documentID 存储在文档中的方法。
I tried following, but it is giving wrong ID not docuemntID:我尝试了以下操作,但它给出了错误的 ID 而不是文档 ID:
const key =firebase.database().ref().push()
Since I don't see any saveChurchData()
method in your code, I make the assumption that instead of doing由于我在您的代码中没有看到任何saveChurchData()
方法,因此我假设不是这样做
const churchResult = await saveChurchData(userEntry)
you wan to do你想做
const churchResult = await saveData(userEntry)
The following would do the trick, by using the doc()
method without specifying any documentPath
:以下将通过使用doc()
方法而不指定任何documentPath
来解决问题:
const userEntry = {
UserName: "",
creationDate: ""
}
const churchResult = await saveData(userEntry)
const saveData = async (data: object) => {
try {
const docRef = database.collection('users').doc();
const docId = docRef.id;
await docRef.set({ UserId: docId, ...data });
return docId;
} catch (error) {
//...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.