你與貓鼬使用的功能在本質上是異步的;變量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