I'm having problems trying to "loop" into multiple "nested" promises with native JS.
I have a JSON with many sublevels as shown:
{
"project": {
"name": "TESTNAME",
"label":"string11405",
"description":"das",
},
"form": [{
"label": "string",
"name": "string",
"fields": [{
"label": "string",
"name": "string",
"field_type_id": "string",
"meta": [{
"meta_name": "string",
"meta_value": "string"
}]
}]
}
So I have checked that I need to use Promise.all
.
return Promise.all(_.map(req.crfData ,(value,index,arr) => {
var table = 'TABLE1';
return conn.query(`INSERT INTO ${table} SET ?`,[value]);
}))
.then((result) => {
var table = 'TABLE2';
return Promise.all(_.map(req.fieldData ,(value,index,arr) => {
value.crf_id = result.insertId;
return conn.query(`INSERT INTO ${table} SET ?`,[value]);
}));
})
.then((result) => {
var table = 'TABLE3';
return Promise.all(_.map(req.fieldData ,(value,index,arr) => {
return conn.query(`INSERT INTO ${table} SET ?`,[value[0]],(err) => next(err));
}));
})
.catch((err) => next(err));
The problem comes with the second query (TABLE2) where I can't retrieve the mysql new id from the previous promise because result
contains a promise and not the appropiate data should be returned.
Thanks for the help.