I'm having hard time with an array update in Node. Here is my problem : I make an API call to receive an array like this
[{id:1 name:x ...}{id:2 name:x ...} ]
I want to pass each line of this array to another query to update the name key (from number to string). Here is my code :
axios(config)
.then(function (response) {
return response.data;
})
.then(function (response) {
return Promise.all(response.myarray.map(entrie => {
const sqlSelect = "SELECT * FROM table WHERE name = ?"
db.query(sqlSelect, entrie.Id, (err, result) => {
const new_name = result[0].name
return { ...entrie, name: new_name }
})
}))
})
.then(function (response) {
res.send(response)
})
.catch(function (error) {
console.log(error);
});
}
I Finally got an array of null values [null, null]
I don't know what's wrong because my SQL request works, the array update works when I use a define value for new_name instead of using SQL request. I feel that it has something to do with the asynchronous nature of Node but can't figure out what's wrong.
Thanks