我有一个在console.log()中返回的变量,但在渲染时返回一个空数组。
function loadFestival(festSlug) {
return Q.Promise(function(resolve, reject, notify) {
Collection.findOne({
slug: festSlug
}).deepPopulate('events shows')
.exec(function(err, model) {
if (err) {
return reject(err);
}
if (!model) {
return reject(new Error('Festival not found for slug: ' + festSlug));
}
var show_performances = [];
model.shows.forEach(function(show) {
var showID = show._id.toString();
show_performances[showID] = []
show_performances[showID]['performances'] = [];
show_performances[showID]['ticketing'] = [];
model.events.forEach(function(event) {
if (show._id.toString() == event.show.toString()) {
show_performances[showID]['performances'].push(moment(event.dateRange.start).format('YYYY MMM D'));
show_performances[showID]['ticketing'][moment(event.dateRange.start).format('YYYY MMM D')] = event.ticketing_url;
}
});
});
resolve(show_performances);
});
});
}
来自评论:
//Here is the code running
loadFestival('slug').then(function(model) {
console.log(model) //returns as should be
res.render('.//index.html', {
layout: layout,
model: JSON.stringify(model) // returns nothing
});
});
上面的代码应该在console.log(show_performances)中返回,但是当渲染时没有返回。感谢。