[英]jade using javascript variable (mongo model)
I am using node.js to create website and trying to use data model created in js file. 我正在使用node.js创建网站,并尝试使用js文件中创建的数据模型。
Even after res.render() is called in the routes/browse.ts, the result from the iteration in the jade file wont show up as the result in the webpage created. 即使在route / browse.ts中调用了res.render()之后,玉文件中的迭代结果也不会显示在创建的网页中。 It seems like there is something wrong with how I am iterating in each-in part of the jade fileAlthough console.log shows that my comicArray in browse.ts is not empty.
尽管我在jade文件的每个部分中进行迭代的方式似乎有问题,但console.log表明我在browser.ts中的comicArray不为空。 here is my code you can refer to:
这是我的代码,您可以参考:
routes/browse.ts: 路线/ browse.ts:
// Comic GET request
router.get('/', function(req, res) {
// Saving found comics into the array named 'comics'
comic.comicModel.find({}, function(err, comics){
if (err || !comics) throw err;
res.locals.comicArray = comics.slice(0, 10);
comicArray = res.locals.comicArray;
});
res.render('browse', { title: 'Browse', comicArray: comicArray});
});
views/browse.jade: 意见/ browse.jade:
extends layout
block content
.row.column.text-centent
h1= title
.listbox
ul
each val in comicArray
li= val.title
models/Comic.ts (schema for Comic): 模型/Comic.ts(漫画模式):
export var comicSchema = new mongoose.Schema({
title: String,
description: String,
genre: String,
thumbnail: String,
cells: Array,
votes: Number,
views: Number,
comments: Array
});
comic.comicModal.find()
is an asynchronous function. comic.comicModal.find()
是一个异步函数。 You need to move the res.render()
call inside that callback. 您需要在该回调中移动
res.render()
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.