从moongoose数据库显示到index.jade

时间:2018-02-22 13:32:14

标签: node.js mongodb express mongoose pug

我正在使用nodejs,express和jade模板引擎来显示从我的数据库到index.jade文件的书籍,我希望数据库中的所有书籍都显示在index.jade页面上

我在下面创建了一本书模型

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/nodeauth');

var db = mongoose.connection;

var bookSchema = mongoose.Schema({
    title: String,
    category: String,
    description: String,
    author: String,
    publisher: String,
    price: Number,
    stock: Number,
    cover: String
});

var Book = module.exports = mongoose.model(' Book',bookSchema,' books');

我在index.js中调用了我的bookModel下面在get请求中呈现了index.jade视图

var express = require('express');
var router = express.Router();

var Book = require('../models/bookModel');

/* GET home page. */
router.get('/', function(req, res, next) {
    Book.find({}, function (err, books) {
        if (err) {console.log(err);}

        res.render('index', {books: books}, { title: 'Members' });
    });
});

module.exports = router;

问题是它一直在运行无限循环而不显示任何

这是我的index.jade

extends layout

block content
    .content
       each book in books
           .book
               img(src="/images/book.cover", width="150px", height="200px")
               h4.book-title book.title
               h6.book-dtls Price: book.price SDG
               p.book-desc book.description
               a.btn.btn-primary.view(href="/books/details/book._id") View Book

1 个答案:

答案 0 :(得分:0)

首先使用#{book.title}显示关于哈巴狗的数据

 img(src="/images/book.cover", width="150px", height="200px")
           h4.book-title #{book.title}
           h6.book-dtls Price: #{book.price} SDG
           p.book-desc #{book.description}
           a.btn.btn-primary.view(href="/books/details/book._id") View Book

可能是book.books中存在问题。

首先,您应该使用console.log(book)来检查数据

`