你与猫鼬使用的功能在本质上是异步的;变量vid
和pap
当您运行res.render
不被初始化。当您尝试在前端使用这些变量(如翡翠,把手EJS模板,我不知道你使用的是什么),他们是不确定的,并随后导致500错误。您需要运行这些函数,以便运行时所有Mongoose查询的结果可用于res.render
;要么使用异步NodeJS库,要么在每个函数内调用另一个函数,然后在最后调用res.render。
var async = require('async');
async.parallel([
// Each function in this array will execute in parallel
// The callback function is executed once all functions in the array complete
function (cb) {
Videos.find({}, function(err, videos) {
if (err) {
return cb(err);
} else {
return cb(null, videos);
}
});
},
function (cb) {
Papers.find({}, function(err, papers) {
if (err) {
return cb(err);
} else {
return cb(null, papers);
}
});
},
function (cb) {
Material.findById(req.params.id, function(err, found) {
if (err) {
return cb(err);
} else {
return cb(null, found);
}
});
}
], function (err, results) {
if (err) {
// If any function returns an error
// (first argument), it will be here
console.log(err);
}
else {
// Even though the functions complete asynchronously,
// the order in which they are declared in the array
// will correspond to the position in the array
// if it returns anything as a second argument.
var videos = results[0];
var files = results[1];
var found = results[2];
res.render('files', {
file: files,
video: videos,
current: found
});
}
});
解决方案2:嵌套回调
Videos.find({}, function(err, videos) {
var vid = videos;
if (err) {
console.log(err);
} else {
Papers.find({}, function(err, file) {
var pap = file;
if (err) {
console.log(err);
} else {
Material.findById(req.params.id, function(err, found) {
if (err) {
console.log(err);
} else {
res.render('files', {
file: pap,
video: vid,
current: found
});
}
});
}
});
}
});
自然的错误谁在JS世界潜水。请记住:所有必须等待第三方响应的内容都是异步完成的。它可以是数据库查询请求,一些API请求,一些手表的事件等。(: – num8er
当我改变的变量,从地方到全球 –
对于你的运气,它只是时间里从数据库中的所有答复进来的时候))) – num8er