简体   繁体   中英

jade using javascript variable (mongo model)

I am using node.js to create website and trying to use data model created in js file.

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. 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. here is my code you can refer to:

routes/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:

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):

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. You need to move the res.render() call inside that callback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM