更新时间:2023-02-18 13:42:59
Async.js实用工具有许多好东西,包括地图功能和 underscores.js 帮助整理任何内容!
The Async.js utility has lots of good stuff including a map function and underscores.js helps tidy anything up!
app.get('/viewing/:id', function (req, res){
if(!req.cookies.user) {
res.end('Requires Authenticated User');
}
else {
connection.query('SELECT something,somethingelse from mytable where userId = ?',[req.params.id], function (error, rows, fields) {
async.map(rows, getUsers, function(err, results){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(_.flatten(_.compact(results))));
});
});
}
});
function getUsers(user, callback) {
connection.query('SELECT id,firstName,lastName FROM users WHERE id = '+ user.otherId, function(err, info) {
if(err) {
console.log(err);
return callback(err);
}
else {
return callback(null, info);
}
});
}