我有一个GETTER NodeJS API(NodeJS和MongoDB)。
//! Add Json Support: Tested
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/api/settings/otherusers/get/:userid', function(req , res){
//! Extract Main User Details Details*/
var user = req.params.userid;
console.log("[User-PErmission-Query-Request][TimeStamp:" + new Date().toISOString() + "]" + user);
//! Insert the registration record in DataBase*/
requests.getpermission(res, user , function (found) {
if(found != null)
{
res.json(found);
res.end();
}
});
});
因为它应该从MongoDB中获取结果,所以我设计了一个函数,它在内部调用另一个函数,它实际上返回调用函数导出的CALLBACK中的结果。
我的通话功能:
//! Allowing User to Query all dependent registered user and their permissions
exports.getpermission = function(res, user, callback){
var result = {};
result.other_user = [];
result.other_user_permissions = [];
RetrieveUserDetails(user,
function(err, user, current, totaluser){
if(!err)
{
result.other_user.push(user);
}
else
{
//! End Processing
callback({
"user":
{
"responseCode":"404",
"userId": user,
"messasge": "No Record Found"
}
}
);
}
},
function(err, permit, current, totalpermit){
if(!err)
{
result.other_user_permissions.push(permit);
}
else
{
//! End Processing
callback({
"user":
{
"responseCode":"404",
"userId": user,
"messasge": "No Record Found"
}
}
);
}
},
function(status)
{
if(true == status)
{
//! JSON Response
var json_response = {};
json_response.responseCode = 200; //! Okay Response
var otherusers = []
json_response.otherusers = otherusers; //! Other users
//! Logging all users
for (var i = 0; i < result.other_user.length; i++){
var otheruser = {
"otheruserId": result.other_user[i].other_user_id,
"otheruserName": result.other_user[i].other_name,
"permissions": []
};
//! Logging all permissions
for (var j = 0; j < result.other_user_permissions.length; j++){
if(result.other_user[i].other_user_id == result.other_user_permissions[j].other_user_id)
{
otheruser.permissions.push({"alert_type": result.other_user_permissions[j].alert_type,
"value": result.other_user_permissions[j].value
});
}
}
json_response.otherusers.push(otheruser);
}
res.json(json_response);
res.end();
//callback(JSON.stringify(json_response, null, 2));
//console.log("##########################################");
console.log(JSON.stringify(json_response, null, 2));
//! End Processing
}
else
{
//! End Processing
callback({
"user":
{
"responseCode":"404",
"userId": user,
"messasge": "No record found"
}
}
);
}
});
}
我的被叫功能:
function RetrieveUserDetails(user, callbackuser, callbackpermit, callbackend)
{
var i = 0; /*Users*/
var j = 0; /*Permissions*/
var totalusers = 0; /*total number of users*/
var totalpermissions = 0; /*total number of permisions*/
//! Check if the user already exists in Database*/
msil_users.find({user_id: user}, function(err,users){
//! A given user with same phone number exists*/
if((users.length != 0) && (!err))
{
console.log("\n[User-Found]: "+ "UserName: "+ users[0].f_name + " User-ID: "+ users[0].user_id);
//! Check for all registered users for a given primary user
msil_other_users.find({parent_user_id: user}).exec(function(err,users){
totalusers = users.length;
//! Update other_user array
for(i = 0; i< users.length; i++)
{
//Send User Information
callbackuser(null, users[i], i, users.length);
//! Check for all registered users for a given primary user
msil_other_users_permit.find({other_user_id: users[i].other_user_id}).exec(function(err,permission){
totalpermissions = permission.length;
//! Update other_user array
for(j = 0; j < permission.length; j++)
{
callbackpermit(null, permission[j] , j, permission.length);
//! End Criteria
if((i >= (totalusers-1)) && (j >= (totalpermissions-1)))
{
console.log("CurrentUser- "+ i + " CurrentPermission-"+ j + " TotalUser-"+ totalusers + " TotalPermissions-"+ totalpermissions);
return callbackend(true);
}
}
});
}
});
}
else
{
callbackuser("No User Exists", null);
callbackpermit("No Permission Exists", null);
return callbackend(false);
}
});
}
我在控制台中输出两次(第一次是错误的,第二次是正确的)。我无法弄清楚为什么我会得到两个输出。
[User-PErmission-Query-Request][TimeStamp:2016-05-09T21:06:27.899Z]Abhishek98304
75278
[User-Found]: UserName: Abhishek User-ID: Abhishek
CurrentUser- 3 CurrentPermission-1 TotalUser-3 TotalPermissions-2
{
"responseCode": 200,
"otherusers": [
{
"otheruserId": "BharatiDhar",
"otheruserName": "BharatiDhar",
"permissions": [
{
"alert_type": "intrusion_alert",
"value": "ON"
},
{
"alert_type": "eCall",
"value": "ON"
}
]
},
{
"otheruserId": "PrithaMoulik",
"otheruserName": "PrithaMoulik",
"permissions": []
},
{
"otheruserId": "FarhatRahaman",
"otheruserName": "FarhatRahaman",
"permissions": []
}
]
}
##########################################
GET /api/settings/otherusers/get/Abhishek 200 33ms - 392b
CurrentUser- 3 CurrentPermission-1 TotalUser-3 TotalPermissions-2
{
"responseCode": 200,
"otherusers": [
{
"otheruserId": "BharatiDhar",
"otheruserName": "BharatiDhar",
"permissions": [
{
"alert_type": "intrusion_alert",
"value": "ON"
},
{
"alert_type": "eCall",
"value": "ON"
}
]
},
{
"otheruserId": "PrithaMoulik",
"otheruserName": "PrithaMoulik",
"permissions": []
},
{
"otheruserId": "FarhatRahaman",
"otheruserName": "FarhatRahaman",
"permissions": [
{
"alert_type": "eCall",
"value": "ON"
},
{
"alert_type": "intrusion_alert",
"value": "ON"
}
]
}
]
}
##########################################
请帮忙,我无法解决这个问题。我来自C ++背景,而不熟悉NodeJS。