如果路径已知,检查 Firestore 记录是否存在的最佳方法是什么?
What's the best way to check if a Firestore record exists if its path is known?
给定一个给定的 Firestore 路径,检查该记录是否存在的最简单和最优雅的方法是什么?创建一个可观察的文档并订阅它?
正在看this question it looks like .exists
can still be used just like with the standard Firebase database. Additionally, you can find some more people talking about this issue on github here
新示例
var docRef = db.collection("cities").doc("SF");
docRef.get().then((doc) => {
if (doc.exists) {
console.log("Document data:", doc.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch((error) => {
console.log("Error getting document:", error);
});
旧示例
const cityRef = db.collection('cities').doc('SF');
const doc = await cityRef.get();
if (!doc.exists) {
console.log('No such document!');
} else {
console.log('Document data:', doc.data());
}
Note: If there is no document at the location referenced by docRef, the resulting document will be empty and calling exists on it will return false.
旧示例 2
var cityRef = db.collection('cities').doc('SF');
var getDoc = cityRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
} else {
console.log('Document data:', doc.data());
}
})
.catch(err => {
console.log('Error getting document', err);
});
检查这个:)
var doc = firestore.collection('some_collection').doc('some_doc');
doc.get().then((docData) => {
if (docData.exists) {
// document exists (online/offline)
} else {
// document does not exist (only on online)
}
}).catch((fail) => {
// Either
// 1. failed to read due to some reason such as permission denied ( online )
// 2. failed because document does not exists on local storage ( offline )
});
我最近在使用 Firebase Firestore 时遇到了同样的问题,我使用了以下方法来克服它。
mDb.collection("Users").document(mAuth.getUid()).collection("tasks").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
if (task.getResult().isEmpty()){
Log.d("Test","Empty Data");
}else{
//Documents Found . add your Business logic here
}
}
}
});
task.getResult().isEmpty() 提供了解决方案,即是否找到了针对我们查询的文档
根据您使用的库,它可能是可观察的而不是承诺。只有承诺才会有 'then' 声明。您可以使用 'doc' 方法而不是 collection.doc 方法,或 toPromise() 等。这是一个使用 doc 方法的示例:
let userRef = this.afs.firestore.doc(`users/${uid}`)
.get()
.then((doc) => {
if (!doc.exists) {
} else {
}
});
})
希望这对您有所帮助...
如果模型包含太多字段,最好在 CollectionReference::get()
结果上应用字段掩码(让我们保存更多 google 云流量计划,\o/)。因此,最好选择仅使用 CollectionReference::select()
+ CollectionReference::where()
到 select 我们想从 firestore 获得的内容。
假设我们有与 firestore cities example 相同的集合架构,但在我们的文档中有一个 id
字段具有与 doc::id
相同的值。然后你可以这样做:
var docRef = db.collection("cities").select("id").where("id", "==", "SF");
docRef.get().then(function(doc) {
if (!doc.empty) {
console.log("Document data:", doc[0].data());
} else {
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
现在我们只下载 city::id
而不是下载整个文档来检查它是否存在。
如果出于某种原因您想在 angular 中使用 observable 和 rxjs 而不是 promise:
this.afs.doc('cities', "SF")
.valueChanges()
.pipe(
take(1),
tap((doc: any) => {
if (doc) {
console.log("exists");
return;
}
console.log("nope")
}));
给定一个给定的 Firestore 路径,检查该记录是否存在的最简单和最优雅的方法是什么?创建一个可观察的文档并订阅它?
正在看this question it looks like .exists
can still be used just like with the standard Firebase database. Additionally, you can find some more people talking about this issue on github here
新示例
var docRef = db.collection("cities").doc("SF");
docRef.get().then((doc) => {
if (doc.exists) {
console.log("Document data:", doc.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch((error) => {
console.log("Error getting document:", error);
});
旧示例
const cityRef = db.collection('cities').doc('SF');
const doc = await cityRef.get();
if (!doc.exists) {
console.log('No such document!');
} else {
console.log('Document data:', doc.data());
}
Note: If there is no document at the location referenced by docRef, the resulting document will be empty and calling exists on it will return false.
旧示例 2
var cityRef = db.collection('cities').doc('SF');
var getDoc = cityRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
} else {
console.log('Document data:', doc.data());
}
})
.catch(err => {
console.log('Error getting document', err);
});
检查这个:)
var doc = firestore.collection('some_collection').doc('some_doc');
doc.get().then((docData) => {
if (docData.exists) {
// document exists (online/offline)
} else {
// document does not exist (only on online)
}
}).catch((fail) => {
// Either
// 1. failed to read due to some reason such as permission denied ( online )
// 2. failed because document does not exists on local storage ( offline )
});
我最近在使用 Firebase Firestore 时遇到了同样的问题,我使用了以下方法来克服它。
mDb.collection("Users").document(mAuth.getUid()).collection("tasks").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
if (task.getResult().isEmpty()){
Log.d("Test","Empty Data");
}else{
//Documents Found . add your Business logic here
}
}
}
});
task.getResult().isEmpty() 提供了解决方案,即是否找到了针对我们查询的文档
根据您使用的库,它可能是可观察的而不是承诺。只有承诺才会有 'then' 声明。您可以使用 'doc' 方法而不是 collection.doc 方法,或 toPromise() 等。这是一个使用 doc 方法的示例:
let userRef = this.afs.firestore.doc(`users/${uid}`)
.get()
.then((doc) => {
if (!doc.exists) {
} else {
}
});
})
希望这对您有所帮助...
如果模型包含太多字段,最好在 CollectionReference::get()
结果上应用字段掩码(让我们保存更多 google 云流量计划,\o/)。因此,最好选择仅使用 CollectionReference::select()
+ CollectionReference::where()
到 select 我们想从 firestore 获得的内容。
假设我们有与 firestore cities example 相同的集合架构,但在我们的文档中有一个 id
字段具有与 doc::id
相同的值。然后你可以这样做:
var docRef = db.collection("cities").select("id").where("id", "==", "SF");
docRef.get().then(function(doc) {
if (!doc.empty) {
console.log("Document data:", doc[0].data());
} else {
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
现在我们只下载 city::id
而不是下载整个文档来检查它是否存在。
如果出于某种原因您想在 angular 中使用 observable 和 rxjs 而不是 promise:
this.afs.doc('cities', "SF")
.valueChanges()
.pipe(
take(1),
tap((doc: any) => {
if (doc) {
console.log("exists");
return;
}
console.log("nope")
}));