检查我的代码也适合你。
router.all('/setLoginDetails', function (req, res) {
if(req.method == 'POST')
{
var Req = req.body;
}
else
{
var Req = req.query;
}
if(Req.lang == undefined || Req.lang == 'en')
{
const message = require('../lang/messages_en.json');
}
if(Req.id == undefined || Req.id == '')
{
return res.json({"success" : false, "Message" : message.REQUIRED , "data" : {} });
}
qb.select('id')
.where({id: Req.id})
.get('table', (err,rows) => {
if (err || rows.length == 0)
{
return res.json({"success" : false, "Message" : "No data found", "data" : Response});
}
else
{
_.each(rows, function(record) {
var token = tokenGenerate();
qb.update('token', {token: token}, {driver_id:record.id}, (err, result) => {
if (err) return console.error(err);
});
qb.select('*').where({driver_id: record.id}).get(model.DriverLogin, (err,rows) => {
var lat = Req.lat;
var lng = Req.lng;
if(Req.lat == '')
{
lat = 0.0;
}
if(Req.lng == '')
{
lng = 0.0;
}
var updateData = {lat : lat ,lng : lng, status : 'free', screen : Req.device_info, driver_id : record.id};
if(rows.length > 0)
{
qb.update('localtion', updateData, {driver_id:record.id}, (err, result) => {
if (err) return console.error(err);
});
}
else
{
qb.insert(model.DriverLogin, updateData, (err, res) => {
if (err) return console.error(err);
});
}
});
});
return res.json({"success" : true, "Message" : "Data Updated", "data" : Response});
}
}
);
});