Ditanya dan dijawab.
Utilitas Async.js memiliki banyak hal bagus termasuk fungsi peta dan underscores.js membantu merapikan apa pun!
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);
}
});
}