title: javascript Map to Firestore -> Error: Value for argument "data" is not a valid Firestore document. Input is not a plain JavaScript obj
body: <p>trying to set map variable value in javascript to firestore document as map field using cloud function. It fails with error - Error: Value for argument &quot;data&quot; is not a valid Firestore document. Input is not a plain JavaScript object (found in field.<br />
  map_field: myMap is causing the problem. So how do I set javascript map as map field in firestore document?</p>
  <pre><code>admin.firestore().collection(&quot;users&quot;).doc(&quot;test&quot;).set({
                                            name: &quot;Los Angeles&quot;,
                                            state: &quot;CA&quot;,
                                            map_field: myMap // map variable
                                        })
  </code></pre>
  <p>Full Code here:
  I am aggregating data from multiple firestore documents and trying to insert into a new document.</p>
  <p>Code-------------------------&gt;</p>
  <pre><code>exports.fxname= functions.https.onRequest((request, response) =&gt; {
   collectionRef = admin.firestore().collection('user/doccx/collx');
  
  let myMap = new Map();
  
  collectionRef.listDocuments().then(documentRefs =&gt; {
                            console.log('Fetching each document:console log');
                            return  admin.firestore().getAll(...documentRefs)
                            .then(documentSnapshots =&gt; {
                                  for (let documentSnapshot of documentSnapshots) {
                                   let name = documentSnapshot.get('aa');
                                   let totalScore= documentSnapshot.get('bb');
                                  
                                   myMap.set(name, totalScore.toString());
                                  }
                                  functions.logger.log(&quot;calculation done: &quot;);
                                  console.log(Array.from(myMap) ); //prints values
                                  var keys = Object.keys(myMap);
                                  keys.forEach(key=&gt;{
                                    console.log(key + '|' + myMap[key]); //this is not printing any value
                                  });
  
                                   admin.firestore().collection(&quot;users&quot;).doc(&quot;test&quot;).set({
                                            name: &quot;Los Angeles&quot;,
                                            state: &quot;CA&quot;,
                                            country: myMap
                                        }).then(function() {
                                             functions.logger.info(&quot;Insert Successful !&quot;, {structuredData: true});
                                              return;
                                          })
                                          .catch(function(error) {
                                              functions.logger.error(&quot;Error writing document: &quot;, error);
                                          });
  
  
                                  return;
                             }).catch(function(error) {
                                functions.logger.error(&quot;Error 1: &quot;, error);
                                });
                         }).catch(function(error) {
                               functions.logger.error(&quot;Error 2: &quot;, error);
                             });
   response.send(&quot;Hello from Firebase Outside!&quot;);`enter code here`
  
  });
  </code></pre>
  <p>Unrelated with the issue but also getting below warning. It has to do with nesting of .then. any suggestion on rewriting above code?</p>
  <p>108:34  warning  Avoid nesting promises          promise/no-nesting</p>
  <p>108:34  warning  Avoid nesting promises          promise/no-nesting</p>