Javascript Nodejs变量在控制台上打印,但不在视图上打印
代码是这样的Javascript Nodejs变量在控制台上打印,但不在视图上打印,javascript,node.js,express,sails.js,Javascript,Node.js,Express,Sails.js,代码是这样的 module.exports = { index: function (req, res, next) { //get an array of all users in user collection Notification.find(function foundNotification(err, notifications) { if (err) return next(err); var elusuario=[];
module.exports = {
index: function (req, res, next) {
//get an array of all users in user collection
Notification.find(function foundNotification(err, notifications) {
if (err) return next(err);
var elusuario=[];
User.findOne(2, function foundUser (err, user) {
if (err) return next(err);
if (!user) return next();
console.log(user);
console.log("----------------------------");
elusuario = user;
console.log(elusuario);
});
res.view({
notifications: notifications,
elusuario: elusuario
});
})
}
})
这是控制器,在控制台中打印的是很好的,但在视图中用户没有值。为什么?
我认为是与全局变量有关的东西。但是我不知道
谢谢
编辑
好的,所以这个方法是异步的。我要做的是通过user.id查找通知和用户,并获取user.name,如果我这样做呢
module.exports = {
index: function (req, res, next) {
//get an array of all users in user collection
Notification.find(function foundNotification(err, notifications) {
if (err) return next(err);
User.find(function foundUser (err, users) {
if (err) return next(err);
var usuarios_locotes = [];
_.each(notifications, function (notification) {
_.each(users, function (user) {
if(notification.token_user==user.token){
console.log(user.token);
usuarios_locotes.push(user);
console.log(usuarios_locotes);
};
});
});
res.view({
notifications: notifications,
users: usuarios_locotes
});
});
})
}
})
它还不起作用吗?每个都是一个异步函数
对于所有这些可能愚蠢的问题,我深表歉意。当代码运行时,在调用
res.view
之前,不会执行函数foundNotification
。我给你的建议是读一些关于承诺的书
您可以按以下方式更改代码以正常工作:
function (req, res, next) {
//get an array of all users in user collection
Notification.find(function foundNotification(err, notifications) {
if (err) return next(err);
var elusuario=[];
User.findOne(2, function foundUser (err, user) {
if (err) return next(err);
if (!user) return next();
console.log(user);
console.log("----------------------------");
elusuario = user;
console.log(elusuario);
res.view({
notifications: notifications,
elusuario: elusuario
});
});
});
}
代码运行时,在调用
res.view
之前,不会执行函数foundNotification
。我给你的建议是读一些关于承诺的书
您可以按以下方式更改代码以正常工作:
function (req, res, next) {
//get an array of all users in user collection
Notification.find(function foundNotification(err, notifications) {
if (err) return next(err);
var elusuario=[];
User.findOne(2, function foundUser (err, user) {
if (err) return next(err);
if (!user) return next();
console.log(user);
console.log("----------------------------");
elusuario = user;
console.log(elusuario);
res.view({
notifications: notifications,
elusuario: elusuario
});
});
});
}
findOne方法是一种异步方法,因此它在执行时不向res.view提供适当的数据
尝试将整个逻辑封装在同一个函数中,它可能看起来很难看,但现在就可以了findOne方法是一种异步方法,因此它在执行时没有向res.view提供适当的数据
尝试将整个逻辑封装在同一个函数中,它可能看起来很难看,但它现在就可以了。
User
对象的方法findOne
异步运行。因此,在findOne
返回用户对象之前渲染视图
如果将
console.log
放在render.view
之前,它将在console.log
内部findOne
方法之前打印输出。User
对象的findOne
方法异步运行。因此,在findOne
返回用户对象之前渲染视图
如果将
console.log
放在render.view
之前,它将在console.log
内部findOne
方法之前打印输出。好吧。。首先真的感谢大家。我解决这个问题
我知道这不是一个正确的方法,但它是有效的,所以对于我的提议来说,这很好
编辑后的问题是,在视图中,我试图编写一个带有参数的对象,但我发送的是向量的向量,因此更改此行:
usuarios_locotes.push(new Object(users[h]));
我可以发送一个物体向量
所以。。无论如何,谢谢你,因为以后我会修改我的代码,让它做得更好、更高效
这是我的第一篇帖子,很抱歉没有读到如何使用这个哈哈的第一步,因为我想我已经犯了很多错误
对不起我的英语:好吧。。首先真的感谢大家。我解决这个问题 我知道这不是一个正确的方法,但它是有效的,所以对于我的提议来说,这很好 编辑后的问题是,在视图中,我试图编写一个带有参数的对象,但我发送的是向量的向量,因此更改此行:
usuarios_locotes.push(new Object(users[h]));
我可以发送一个物体向量
所以。。无论如何,谢谢你,因为以后我会修改我的代码,让它做得更好、更高效
这是我的第一篇帖子,很抱歉没有读到如何使用这个哈哈的第一步,因为我想我已经犯了很多错误
对不起,我的英文是:C对不起,我刚刚编辑了这个问题,你知道为什么这个问题不起作用吗?:关于
。。每一个,我都找到了另一个对你有更好帮助的答案:对不起,我刚刚编辑了这个问题,你知道为什么这个问题不起作用吗?:关于。,我找到了另一个对你有更好帮助的答案: