I am new to fetch and react.js and basically javascript, I am trying to create a new row in a table and just get an ID back (second then), the problem is that temp in the then domain is changing but its not changing outside it although I defined it as function level variable
handleAddRow({ newRowIndex }) {
var temp = null;
/////////////////////////////////////// updating records in db
fetch(`${config.serverUrl}/criteriaAPI`,
{
method: "POST",
dataType: 'json',
headers: {
'Accept': 'application/json; charset=UTF-8',
'Content-Type': 'application/json; charset=UTF-8'
}
})
.then(function(res){ return res.json(); })
.then(function(data){ temp = data._id ;alert(temp)})
//////////////////////////////////////
console.log(temp);
const newRow = {
_id: temp,
criteria_id: '',
securityCriteria: '',
description: '',
};
let rows = this.state.rows.slice();
rows = update(rows, {$push: [newRow]});
},
console.log(temp) = > null
alert(temp) = > id key : id value