Reputation: 161
I have a api code like below is written via node.js. Node.js is asynchronous so next step executing before db query result. In that time system had not got the data so gives an error. I have searched callback structure, promise and await but could not solve it. Thanks.
var dateTime = require("node-datetime");
var dt = dateTime.create();
module.exports = {
myOrderImportFromFileAPI: (req, res) => {
//let age = req.params.Age;
// send the player's details to the database
let userName = req.body.userName;
let companyID = req.body.companyId;
let channelId = req.body.channelId;
let orders = req.body.orders;
//default values
let orderTypeId = 1;
let orderStatusId = 1;
let createdDate = dt.format("Y-m-d H:M:S");
let getDate = dt.format("Y-m-d H:M:S");
db.query(`select ID,Code from operationcenters where CompanyID=${companyID}`, (err, result) => {
if (err) {
return res.status(500).send(err);
}
var operationCenterId = result[0].ID
result.find((e) => console.log(e.ID));
});
console.log("operationCenterId");
console.log(operationCenterId);
console.log("channelId");
console.log(channelId);
console.log(orders);
let query = "select ID value, Name label from channels"
db.query(query, (err, result) => {
if (err) {
return res.status(500).send(err);
}
res.send(`1`);
});
},
};
Upvotes: 0
Views: 514
Reputation: 1588
const dateTime = require("node-datetime");
const dt = dateTime.create();
const myOrderImportFromFileAPI = async (req, res) => {
const { userName, companyID, channelId, orders } = req.body
const orderTypeId = 1;
const orderStatusId = 1;
const createdDate = dt.format("Y-m-d H:M:S");
const getDate = dt.format("Y-m-d H:M:S");
try {
const queryData = await queryDatabase()
res.send(queryData).status(200)
} catch(error) {
res.send(error).status(500)
}
}
function queryDatabase() {
const query = "select ID value, Name label from channels"
return new Promise((resolve, reject) => {
db.query(query, (err, result) => {
if (err) {
reject(err)
}
resolve(result)
});
})
}
soomething like above will get you on the way. I haven't tested this so I can't say it works 100% but it is a good starting point. The reason for this is because by default the mysql db doesn't support asynchronous the modern way. That's why many people wrap in Bluebird(?) which is a library for creating promises which is a frequent dependency on drivers and such. So what you are doing is creating an async function with const myOrderImportFromFileAPI = async (req, res) => {}
After that then creating your variables, since you aren't redeclaring them then use const
the first line after the function declaration is destructuring the object. Since we know the name of data and naming it as such then you can save lines with that and makes it more readible.
Finally, we wrap the async call in a try catch statement since we are calling a function which returns a promise(we don't need to call name async function queryDatabase
since we are not performing anything asynchronous inside)
inside the actual function queryDatabase
we will return with a resolved value of result
or return a rejected value of err
which will be caught in our try catch statment and will send the appropriate response..
Upvotes: 1