从Firestore获取地图字段的地图 [英] Get map of map field from Firestore

查看:49
本文介绍了从Firestore获取地图字段的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个map类型的字段,其中包含firestore中的数据地图.

I have a field of type map that contains Maps of data in firestore.

我正在尝试使用node.js中的云函数检索此数据.我可以从现场获取文档和数据,但无法以可用的方式获取.我已经尝试了所有可以在SO和google上找到的解决方案,但是以下是唯一可以让我访问数据的代码.我显然需要能够分别访问Map中的每个字段.我很快建立了一个String:Any数组,但是我可以在Node.JS中使用它

I am trying to retrieve this data using a cloud function in node.js. I can get the document and the data from the field but i can't get it in a usable way. I have tried every solution i can find on SO and google but the below is the only code that can give me access to the data. I obviously need to be able to access each field with in the Map individually. in swift i build an array of String:Any but i can get that to work in Node.JS

const docRef = dbConst.collection('Comps').doc('XEVDk6e4AXZPkNprQRn5Imfcsah11598092006.724980');

return docRef.get().then(docSnap => {

 const tagets = docSnap.get('targets')

 console.log(tagets);

}).catch(result => { console.log(result) });

这就是我在控制台中得到的.

this is what i am getting back in the console.

在Swift中,我执行以下操作,到目前为止,我无法在打字稿中找到等效项.(我不需要构建自定义对象就可以访问键和值)

In Swift i do the following and am so far not able to find an equivalent in typescript. (i don't need to build the custom object just ability to access the keys and values)

let obj1 = doc.get("targets") as! [String:Any]
                        
                        for objs in obj1{
                            let obs = objs.value as! [String:Any]
                            let targObj = compUserDetails(IDString: objs.key, activTarg: obs["ActivTarget"] as! Double, stepTarg: obs["StepTarget"] as! Double, name: obs["FullName"] as! String)

更新花了整整一天的时间后,我认为我可以使用以下解决方案:

UPDATE After spending a whole day working on it thought i had a solution using the below:

const docRef = dbConst.collection('Comps').doc('XEVDk6e4AXZPkNprQRn5Imfcsah11598092006.724980');

return docRef.get().then(docSnap => {
const tagets = docSnap.get('targets') as [[string, any]];
const newDataMap = [];

for (let [key, value] of Object.entries(tagets)) {
  const tempMap = new Map<String,any>();
  console.log(key);

  const newreWorked = value;

  tempMap.set('uid',key);

  for(let [key1, value1] of Object.entries(newreWorked)){
    tempMap.set(key1,value1);
    newDatMap.push(tempMap);
  };

};

  newDatMap.forEach(element => {
    const name = element.get('FullName');
    console.log(name);
  });

但是,新数据映射具有6个单独的映射对象.来自云的每个原始对象中的3个.我现在可以遍历并获取给定密钥的数据,但是我有3倍的对象.

However the new data map has 6 seperate mapped objects. 3 of each of the original objects from the cloud. i can now iterate through and get the data for a given key but i have 3 times as many objects.

推荐答案

因此,在经过两天的仔细搜索后,我终于找到了解决方案,它与上面的代码非常相似,但是可以正常工作.它可能不是正确的"的方式,但它的工作原理.随时提出其他建议.

So after two days of searching an getting very close i finnaly worked out a solution, it is very similar to the code above but this works. it may not be the "correct" way but it works. feel free to make other suggestions.

return docRef.get().then(docSnap => {
const tagets = docSnap.get('targets') as [[string, any]];
const newDatarray = [];

for (let [key, value] of Object.entries(tagets)) {
  const tempMap = new Map<String,any>();
  const newreWorked = value;
  tempMap.set('uid',key);

  for(let [key1, value1] of Object.entries(newreWorked)){
    tempMap.set(key1,value1);
  };
  newDatarray.push(tempMap);
};

  newDatarray.forEach(element => {
    const name = element.get('FullName');
    const steps = element.get('StepTarget');
    const avtiv = element.get('ActivTarget');
    const UID = element.get('uid');
    console.log(name);
    console.log(steps);
    console.log(avtiv);
    console.log(UID);
  });

 

}).catch(result => { console.log(result) });

这篇关于从Firestore获取地图字段的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆