Я хочу получить последний идентификатор из базы данных при получении API,
проблема в таблице MaterialUnit materialID показывает 0
console.log отлично работает на сервере отлично работает!
вот сервер.js
app.get('/AddProducts', function (req, res) {
const { materialName, description, categoryID, manufactureID, currencyID} = req.query;
const INSERT_MATERIAL = `INSERT INTO material (materialName, description, categoryID, manufactureID, currencyID) VALUES
('${materialName}','${description}','${categoryID}','${manufactureID}','${currencyID}')`;
connection.query(INSERT_MATERIAL, (error, result) => {
if(error) {
return res.send(error)
}
else {
console.log(result.insertId);
return res.json({id: result.insertId})
}
});
});
app.get('/AddMaterialUnits', function (req, res) {
const { materialID, unitID, def, rate, price, vat, bar} = req.query;
const INSERT_MATERIAL = `INSERT INTO materialunit (materialID, unitID, barcodeNo, salePrice, vatValue, isdefault, rate) VALUES
('${materialID}','${unitID}','${bar}','${price}','${vat}','${def}' ,'${rate}')`;
connection.query(INSERT_MATERIAL, (error, result) => {
if(error) {
return res.send(error)
}
else {
return res.send(result)
}
});
});
и затем в компоненте:
AddProduct() {
let url1 = `http://localhost:3000/AddProducts?materialName=${this.state.name}&description=${this.state.description}&categoryID=${this.state.catID}&manufactureID=${this.state.manID}¤cyID=${this.state.curID}`;
fetch(url1)
.catch(error => console.error(error))
.then(alert("Added successfully"))
.then((result) => this.setState({materialID: result.id}))
.then(alert(this.state.materialID));
this.state.materialUnits.map(obj =>
fetch(`http://localhost:3000/AddMaterialUnits?materialID=${this.state.materialID}&unitID=${obj.unit.id}&def=${obj.default.value}&rate=${obj.rate}&price=${obj.price}&vat=${obj.vat}&bar=${obj.barcodeNo}`)
.catch(error => console.error(error))
.then(alert("Added successfully"))
)
}