Javascript Cloud Firestore找不到自定义文档id
如果我尝试获取自定义文档id,则此函数返回false 只有在firebase控制台上输入文档id时,才会返回trueJavascript Cloud Firestore找不到自定义文档id,javascript,google-cloud-firestore,Javascript,Google Cloud Firestore,如果我尝试获取自定义文档id,则此函数返回false 只有在firebase控制台上输入文档id时,才会返回true checkDot() { this.db.firestore.collection(this.DOT).doc(this.DOT).get() .then( doc => { console.log('Data is ', doc.exists); if (doc.exists) { // this.isDotEx
checkDot() {
this.db.firestore.collection(this.DOT).doc(this.DOT).get()
.then( doc => {
console.log('Data is ', doc.exists);
if (doc.exists) {
// this.isDotExist = true;
console.log(doc, 'Colection exists');
}
else {
// new Account Create
console.log('Colection doos not exist');
this.presentConfirm();
}
});
此函数将用户输入存储在数据库中
async createNewAccount() {
// Binding data from user input
const { Company, Fname, Email, Password } = this;
try {
// creating user account
const res = await this.afAuth.auth.createUserWithEmailAndPassword(Email, Password).then(cred => {
// DOT value passed by another page, others from user input
this.db.collection(this.DOT).doc(this.DOT).collection(Company).doc(Fname).set({ Name: Fname });
});
this.showAlert('Succes', 'You have successfully registered!');
this.route.navigate(['']);
console.log(res);
} catch (err) {
this.showAlert('Error', err.message);
// console.dir(err);
}
}
正如您可以从社区中查看此问题一样,有一种特殊的方法可以通过documentId使用查询。方法是:“FieldPath.documentId()” 另一个参考是官方文档,您可以在其中找到以下代码示例,可以将其用作通过ID返回文档的起点
db.collection("collection").where("document", "==", true)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
除此之外,下面还有一个来自社区的问题,提供了更多信息和示例,与您的类似问题相关,可能会对您有所帮助