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!!!`
});
});
breakunconditionally in a loop - that will cause that loop to only execute once. Also, you need to make your function wait until all the calls toset()are complete. You should collect all those promises into an array and usePromise.all()to send a response only after they are all complete.