从内部函数内部分配外部函数的对象 javascript
Assigning an object of outer function from inside of inner function javascript
我无法在内部函数中为结果赋值。有什么办法吗?
我可以控制台记录函数内的结果变量。有人建议我做 promises 。我不知道该怎么做。我是node和express的初学者
router.post("/register",function(req,res){
var firstName = req.body.firstName;
var lastName = req.body.lastName;
var email = req.body.email;
var mobile = req.body.email;
var oauthProvider = req.body.oauthProvider;
var oauthId = req.body.oauthId;
var userType = req.body.type;
if (userType === undefined) {
userType = 1;
}
var result = {};
result.message = "Not edited";
connection.model.query("SELECT `id` FROM `users`" + "WHERE users.email = ?",[email],function(err,rows){
if(err !== null )
{
result.error = err;
result.success = false;
} else if(rows.length >= 1){
result.success = false;
result.message ="User already exists" ;
}
else
{
connection.model.query("INSERT INTO `users`(`first_name`,`last_name`,`email`,`mobile`,`password`,`user_type`) VALUES (?,?,?,?,?,?)",[firstName,lastName,email,mobile,password,userType],function(err,res){
if(err !== null )
{
result.error = err;
result.success = false;
}else{
var user = {
id : res.insertId,
first_name : firstName,
email : email
};
connection.model.query("INSERT INTO `users_profile`(`user_id`) VALUES (?)",[user.id],function(err,profileres){
if(err !== null )
{
result.error = err;
result.success = false;
}else{
if(oauthProvider != undefined && oauthId != undefined){
connection.model.query("INSERT INTO `users_oauth`(`user_id`, `provider`, `provider_uid`) VALUES (?,?,?)",[user.id,oauthProvider,oauthId],function(err,oauthres){
if(err !== null )
{
result.error = err;
result.success = false;
result.message = "couldn't create oauth user";
}
});
}
var token = jwt.sign(user, SECRET, {
expiresIn: "15d" // expires in 15 days
});
// return the information including token as JSON
result.success = true;
result.message = 'successfully registered';
result.token = token;
}
});
}
});
}
res.setHeader('content-type','text/json');
res.send(json_encode(result));
});
});
你应该在回调中发送响应如下:
result.success = true;
result.message = 'successfully registered';
result.token = token;
res.setHeader('content-type','text/json');
res.send(json_encode(result));
而不是在回调之外发送响应。
编辑:
好的,我知道了..它正在处理你的查询资源,所以改变代码如下:
router.post("/register",function(httpreq,httpres){
result.success = true;
result.message = 'successfully registered';
result.token = token;
httpres.setHeader('content-type','text/json');
httpres.send(json_encode(result));
编辑:如您所知,node.js 将全部异步执行,因此在您的情况下,您正在发送响应 res.send b4 回调已完成。所以现在在我的代码中,我在回调完成后发送 res..
我无法在内部函数中为结果赋值。有什么办法吗? 我可以控制台记录函数内的结果变量。有人建议我做 promises 。我不知道该怎么做。我是node和express的初学者
router.post("/register",function(req,res){
var firstName = req.body.firstName;
var lastName = req.body.lastName;
var email = req.body.email;
var mobile = req.body.email;
var oauthProvider = req.body.oauthProvider;
var oauthId = req.body.oauthId;
var userType = req.body.type;
if (userType === undefined) {
userType = 1;
}
var result = {};
result.message = "Not edited";
connection.model.query("SELECT `id` FROM `users`" + "WHERE users.email = ?",[email],function(err,rows){
if(err !== null )
{
result.error = err;
result.success = false;
} else if(rows.length >= 1){
result.success = false;
result.message ="User already exists" ;
}
else
{
connection.model.query("INSERT INTO `users`(`first_name`,`last_name`,`email`,`mobile`,`password`,`user_type`) VALUES (?,?,?,?,?,?)",[firstName,lastName,email,mobile,password,userType],function(err,res){
if(err !== null )
{
result.error = err;
result.success = false;
}else{
var user = {
id : res.insertId,
first_name : firstName,
email : email
};
connection.model.query("INSERT INTO `users_profile`(`user_id`) VALUES (?)",[user.id],function(err,profileres){
if(err !== null )
{
result.error = err;
result.success = false;
}else{
if(oauthProvider != undefined && oauthId != undefined){
connection.model.query("INSERT INTO `users_oauth`(`user_id`, `provider`, `provider_uid`) VALUES (?,?,?)",[user.id,oauthProvider,oauthId],function(err,oauthres){
if(err !== null )
{
result.error = err;
result.success = false;
result.message = "couldn't create oauth user";
}
});
}
var token = jwt.sign(user, SECRET, {
expiresIn: "15d" // expires in 15 days
});
// return the information including token as JSON
result.success = true;
result.message = 'successfully registered';
result.token = token;
}
});
}
});
}
res.setHeader('content-type','text/json');
res.send(json_encode(result));
});
});
你应该在回调中发送响应如下:
result.success = true;
result.message = 'successfully registered';
result.token = token;
res.setHeader('content-type','text/json');
res.send(json_encode(result));
而不是在回调之外发送响应。
编辑: 好的,我知道了..它正在处理你的查询资源,所以改变代码如下:
router.post("/register",function(httpreq,httpres){
result.success = true;
result.message = 'successfully registered';
result.token = token;
httpres.setHeader('content-type','text/json');
httpres.send(json_encode(result));
编辑:如您所知,node.js 将全部异步执行,因此在您的情况下,您正在发送响应 res.send b4 回调已完成。所以现在在我的代码中,我在回调完成后发送 res..