[英]Firebase Cloud Firestore doc.data().includes or .IndexOf doesn't work
I am trying to work with an array, trying to run doc.data().includes
and doc.data().indexOf
on the array I stored on the cloud firestore, the array is the only thing that exists in the doc.我正在尝试使用一个数组,尝试在我存储在云 Firestore 上的数组上运行
doc.data().includes
和doc.data().indexOf
,该数组是文档中唯一存在的东西。
The data is arranged like this数据是这样排列的
0: example@gmail.com
1: firebase@gmail.com
2: new@gmail.com
To get to the data I just do为了得到我刚刚做的数据
firebase.firestore().collection('mails').doc('emails').get().then((doc) => {
if (doc.exists) {
console.log(doc.data()); //prints the list of emails in form of array
doc.data().IndexOf(email); //error IndexOf doesn't exist
doc.data().includes(email); //error includesOf doesn't exist
}
else {
console.log('data doesn't exist');
}
});
To Get Access to doc.data().IndexOf
or doc.data().includes
or any other array related functions, just convert the doc.data()
to array like this:要访问
doc.data().IndexOf
或doc.data().includes
或任何其他与数组相关的函数,只需将doc.data()
转换为数组,如下所示:
var docArray = Object.values(doc.data());
docArray.includes(stringToSearchFor);
or或者
Object.values(doc.data()).includes(stringToSearchFor);
this will allow you to run docArray.includes
and docArray.IndexOf
.这将允许您运行
docArray.includes
和docArray.IndexOf
。 Reason for converting is that firebase returns the entire doc which is an object of objects, you can see this by console.log(doc.data()) and seeing prototype field.转换的原因是 firebase 返回整个文档,这是一个 object 对象,您可以通过 console.log(doc.data()) 看到这个并查看原型字段。 This happens because firestore stores the array a json object meaning the array index numbers you see are not actually index numbers, they just object keys.
发生这种情况是因为 firestore 将数组存储为 json object 意味着您看到的数组索引号实际上不是索引号,它们只是 object 键。
meaning意义
0: example@gmail.com
1: firebase@gmail.com
2: new@gmail.com
where 0, 1, 2
is the index( or as it seems) and example@gmail.com, firebase@gmail.com, new@gmail.com
is value but in firestore that 0, 1, 2
is actually just keys, they are not index.其中
0, 1, 2
是索引(或看起来)和example@gmail.com, firebase@gmail.com, new@gmail.com
, 2 但实际上是密钥库中的值0, 1, 2
他们 fire,不是索引。 So to get either the key(the numbers) or the values( the emails) we just run Object.keys(doc.data())// will return numbers
or Object.values(doc.data())//will return emails
and you will get array that you want and now you can run operations on this array.因此,要获取键(数字)或值(电子邮件),我们只需运行
Object.keys(doc.data())// will return numbers
或Object.values(doc.data())//will return emails
,你会得到你想要的数组,现在你可以在这个数组上运行操作。
I would suggest that you store your data in an Array and not as separate fields as shown by your Firestore database screenshot.我建议您将数据存储在一个数组中,而不是像您的 Firestore 数据库屏幕截图所示那样作为单独的字段。
Your Firestore document currently contains 6 different fields and with doc.data()
you retrieve all the 6 fields in this document as one JavaScript Object: this is why the Array methods cannot work.您的 Firestore 文档当前包含 6 个不同的字段,并且使用
doc.data()
您可以检索此文档中的所有 6 个字段作为一个 JavaScript Object:这就是数组方法无法工作的原因。 If you store this data in one Array (ie one field of type Array) it will be ok.如果您将此数据存储在一个数组中(即一个数组类型的字段),那就没问题了。
You can try the following code:您可以尝试以下代码:
const docRef = firebase.firestore().collection('mails').doc('emails');
docRef
.set({
mails: ['example@gmail.com', 'firebase@gmail.com', 'new@gmail.com'],
})
.then(() => {
return docRef.get();
})
.then((doc) => {
if (doc.exists) {
const email = 'example@gmail.com';
console.log(doc.data().mails);
console.log(doc.data().mails.indexOf(email));
console.log(doc.data().mails.includes(email));
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.