node.js で作業する方法にかなり混乱しています。コールバック、リターン、およびソース コードの実行方法について話しているのです。
私はsails.jsを使用していますが、リンクされているとは思いません.JSが機能する方法だと思います.
ソースコード:
module.exports = function (req, res, callback) {
if (req.session.authenticated) {
// Accept user authentication.
return callback();
} else {
// Not authenticated. Try auto authentication.
if(validator.check([validator.rulesHardwareId(req.param('hardwareId'))])){
Device.findOne({hardwareId: req.param('hardwareId')}, function(err, device){
if(err){
return res.json({message: "You are not permitted to perform this action. You have to connect to the platform before. [Wrong hardwareId or DB error]", data: {err: err}, status: false}, 403);
}
if(device){
// Connect the device.
req.session.authenticated = true;
req.session.from = 'device';
// Search for the device's owner.
User.findOne({id: device.userId}, function(err, user){
if(err){
return res.json({message: "DB error.", data: {err: err}, status: false}, 403);
}
if(user){
// Add data about the user.
req.session.user = user;
return callback();
}else{
return res.json({message: "Device found but device's owner doesn't found.", data: {err: err}, status: false}, 403);
}
});
}else{
return res.json({message: "You are not permitted to perform this action. You have to connect to the platform before. [Wrong hardwareId]", data: {err: err}, status: false}, 403);
}
});
}
return res.json({message: "You are not permitted to perform this action. You have to connect to the platform before. [Give hardwareId ?]", data: {}, status: false}, 403);
}
};
コードはそれほど重要ではありません。問題は、次のメッセージを受け取ったことです。
しかし、アクションは CREATED です。では、callback() を呼び出して返しますが、ソース コードは続きますか? そして最後の行を実行しますか?なんで?わかりません。最後の行を ELSE にすると、「アクションが作成されました」というメッセージが表示されます。
誰かが私に説明できれば.. return キーワードを追加するとこれを防ぐのに役立つと思いましたが、間違っているようです。
ありがとうございました。