Node.js 如何在CloudFireStore中检查文档是否包含属性?
我想知道是否有办法检查Cloud Firestore文档中是否存在属性。类似于Node.js 如何在CloudFireStore中检查文档是否包含属性?,node.js,firebase,google-cloud-firestore,Node.js,Firebase,Google Cloud Firestore,我想知道是否有办法检查Cloud Firestore文档中是否存在属性。类似于document.contains(“property\u name”)或如果存在文档属性。要解决此问题,只需检查DocumentSnapshot对象的空值,如下所示: var yourRef = db.collection('yourCollection').doc('yourDocument'); var getDoc = yourRef.get() .then(doc => { if (
document.contains(“property\u name”)
或如果存在文档属性。要解决此问题,只需检查DocumentSnapshot
对象的空值,如下所示:
var yourRef = db.collection('yourCollection').doc('yourDocument');
var getDoc = yourRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
} else {
if(doc.get('yourPropertyName') != null) {
console.log('Document data:', doc.data());
} else {
console.log('yourPropertyName does not exist!');
}
}
})
.catch(err => {
console.log('Error getting document', err);
});
我想你指的是提问。
仍然无法检查Firestore中是否存在某些字段。但您可以添加另一个值为true/false的字段
查看此链接了解更多信息
您可以使用
中的操作符,就像下面的代码片段一样
const ref = admin.firestore().collection('yourCollectionName').doc('yourDocName')
try {
const res = await ref.get()
const data = res.data()
if (!res.exists) {
if (!("yourPropertyName" in data)) {
// Do your thing
}
} else {
// Do your other thing
}
} catch (err) {
res.send({err: 'Something went terribly wrong'})
}
嗨,戴安!你试过我上面的解决方案了吗?Alex,对不起。。。我做了,但我忘了将其标记为解决方案,但为我工作。谢谢
const ref = admin.firestore().collection('yourCollectionName').doc('yourDocName')
try {
const res = await ref.get()
const data = res.data()
if (!res.exists) {
if (!("yourPropertyName" in data)) {
// Do your thing
}
} else {
// Do your other thing
}
} catch (err) {
res.send({err: 'Something went terribly wrong'})
}