1

I have a nested json which have a key and value is another json with key and value. Below is the json.

        {
            "Testicular Torsion": {
                "What is testicular torsion?": "ABC",
                "Symptoms": "AB",
                "Risks": "AL",
                "Diagnosis": "LK",
                "Treatment": "UY"
            },
             "XYZ": {
                "X": "ABC",
                "Symptoms": "AB",
                "Risks": "AL",
                "Diagnosis": "LK",
                "Treatment": "UY"
            }
    };

What I am trying to do is insert the data in cloud firestore of firebase. Following is the code for the same. But the issue is, only the first key value pair(In this case the Testicular Torsion key and it's value which is another JSON) is getting inserted and not other key value pairs. Why is the case and what needs to be done in the code?

var string_medical_data = JSON.stringify(medical_json);
var json_medical = JSON.parse(string_medical_data);

function abc(poi){
  firestore.collection('medical').doc(poi).set(json_medical[poi])
    .then(() => {
      return console.log("Added");
  })
  .catch((e => {
    console.log('error: ', e);
    return console.log(e);
  }))
}


exports.medical = functions.https.onRequest((request, response) => {

  var problems = [];
  for(var myKey in json_medical) {
   problems.push(myKey);
   break;
}

for(var i=0;i<problems.length;i++){

  // firestore.collection('medical').doc(problems[i]).set(json_medical[problems[i]])

  abc(problems[i]);

}

response.send({
  'fulfillmentText': `Success!!!`
});

});
2
  • You are using break unconditionally in a loop - that will cause that loop to only execute once. Also, you need to make your function wait until all the calls to set() are complete. You should collect all those promises into an array and use Promise.all() to send a response only after they are all complete. Commented Jun 21, 2018 at 16:33
  • Thanks for replying. break was a valid logical error which I have removed now but still I am unable to add the data as nodejs uses asynchronous calls. Can you please tell me the code snippet on how to do the Promise.all asper the above code. Commented Jun 21, 2018 at 21:13

1 Answer 1

10

You'd probably be better off doing this in a batch request. You can commit multiple writes in a single request. If your data has more than 500 entries you'll have to do break it up and do 500 at a time.

exports.medical = functions.https.onRequest((request, response) => {
  var batch = firestore.batch();
  for(var myKey in json_medical) {
    var myKeyRef = firestore.collection('medical').doc(myKey);
    batch.set(myKeyRef, json_medical[myKey]);
  }
  batch.commit().then(function () {
    response.send({
      'fulfillmentText': `Success!!!`
    });
  });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @abraham This solution worked like a charm and I am able to add all my entries at once which I have been unable to add for so many days. Thanks a lot :)
Great! Don't forget to mark the question as answered.
Best answer! Simple and effective. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.