【发布时间】:2016-01-26 11:05:02
【问题描述】:
我正在使用 Azure 移动服务-NodeJS 后端,在编写它时,我总是面临这个疑问 - 让我用下面的代码 sn-p 解释一下
//--------------------------------------------------------------------------
function addUserToDB(request, response){
///some code here
var theUser = request.user;
///get the user's entity object
try {
objAppUser = buildAppUserEntityObj(theUser, request); //for simplicity sake, lets say this is not asynchronous function
}
catch (err) {
console.log ('error in addUserToDB when calling buildAppUserEntityObj'); //****????****
request.respond(statusCodes.BAD_REQUEST, err);
return; // ##????## is a 'return' needed here to avoid the execution of the code below, or should I assume that the function will return once request is responded (request.respond) in above line.
}
....code to add userEntity to DB
//some more code in case of successful try above, can I assume there is no way this code will be reached in case of error in the above try-catch
// ofcourse I can move this code in the 'try' block above, but I am just trying to understand what happens if above try ends in catch block for some reason and there is no 'return' at the end that catch block.
}
//--------------------------------------------------------------------------
function buildAppUserEntityObj(user, request) {
if ( user.level === 'anonymous' ) { //ideally this would be called in above function, but I am putting this here just to throw an example.
console.error('Anonymous User' );
request.respond(statusCodes.BAD_REQUEST, message); //will this request.respond will send the response to client immediately, or will it be passed on as error the try-catch of above 'addUserToDB' function
return; // ##????## also, is 'return' needed here to avoid the execution of the code below,
}
....code to build a User entity object based on some business logic
}
//--------------------------------------------------------------------------
我想,这一切都归结为三个问题:
1.这两个地方是否需要'return'(上面两个函数中用##????##标记?
2. 如果 user.level === 'anonymous'
是否会记录消息(由 //****????**** 标记)
3. request.respond 与 response.send 有什么区别?
我相信这些疑问是因为我缺乏全面的 expressJS 知识,所以当我再次浏览 azure/express.js 文档时,我想我会在这里向专家社区提出我的疑问以获得更清晰的解释。
非常感谢。
【问题讨论】:
标签: node.js azure express azure-mobile-services