简体   繁体   English

检索Firestore文档数组

[英]Retrieve firestore document array

I am trying to retrieve an array of strings from a firestore document. 我正在尝试从Firestore文档中检索字符串数组。 I have tried the following code. 我尝试了以下代码。

firestore.collection("Category").doc(category).get().then(function(doc) {
    if (doc.exists) {
        subcategory = (doc.data().Subcategories);
        array = subcategory.split(",");
        subcategoryDropdown[subcategoryDropdown.length] = new Option(array);     
    } else {
        alert("No such document!");
    } 
});

But when i run the code, it keeps bringing the array as one string separated by a comma. 但是,当我运行代码时,它始终将数组作为一个字符串,并以逗号分隔。

I am getting this: "obj1,obj2,obj3" but I'm trying to get it as an array such as ["Obj1","Obj2","Obj3"]. 我得到这个:“ obj1,obj2, obj3”,我试图将其作为数组,例如[“ Obj1”,“ Obj2”,“ Obj3”]来获取。

I can't seem to split the array into an array with individual strings. 我似乎无法将数组拆分为具有单个字符串的数组。 The split command does not seem to work. split命令似乎不起作用。

The problem, after looking through the console log, was that I was adding the array from the firestore document into another array. 在查看控制台日志后,问题是我正在将阵列从Firestore文档添加到另一个阵列中。

Instead of doing var subcategoryArray = []; 而不是做var subcategoryArray = []; use an empty variable ie. 使用一个空变量,即。 var subcategory; var子类别;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM