Get map of map field from Firestore

断了今生、忘了曾经 提交于 2021-01-07 03:16:23

问题


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

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.

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);
  });

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.


回答1:


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) });



回答2:


I made this into a little function that gets the underlying object from a map:

function getMappedValues(map) {
    var tempMap = {};
    for (const [key, value] of Object.entries(map)) {
        tempMap[key] = value;
    }
    return tempMap;
}

For an object with an array of maps in firestore, you can get the value of the first of those maps like so:

let doc = { // Example firestore document data
    items: {
        0: {
            id: "1",
            sr: "A",
        },
        1: {
            id: "2",
            sr: "B",
        },
        2: {
            id: "3",
            sr: "B",
        },
    },
};

console.log(getMappedValues(doc.items[0]));

which would read { id: '1', sr: 'A' }



来源:https://stackoverflow.com/questions/63577313/get-map-of-map-field-from-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!