I use the auto generated ID when I create my document. After setting, I need to get it.
await admin.firestore().collection("mycollection").doc().set(mydata)
.then(doc => {
console.log(doc.id);
return true;
})
.catch(err => {
return false;
});
The retrieved ID in the log is not the same as the ID in the Firestore Database. I don't understand why.
The retrieved ID in the log is not the same as the ID in the Firestore Database.
This is normal, since the set()
method returns a Promise<void>
(ie a Promise which resolves to an undefined
). Therefore you cannot do doc.id
in the callback function passed to the then()
method, since doc
is undefined
.
You should do as follows:
try {
const docRef = admin.firestore().collection("mycollection").doc();
const docId = docRef.id; //Here you have the value of the id (independently of the fact you call set() later or not)
await docRef.set(mydata);
} catch(err) {
//...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.