试图从mongodb中显示玉器中的数据 [英] trying to display data in jade from mongodb
本文介绍了试图从mongodb中显示玉器中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
试图显示从猫鼬模式到翡翠Temaplate的数据,但是不管我尝试什么,它都无法正常工作,所以请帮助我,谢谢.
trying to display data from mongoose schema to jade temaplate but it dosent work no matter what i try , so please help me and thanks .
首先这是我的书模式模型/book.js
first here is my book schema models/book.js
const mongoose = require('mongoose')
const schema = mongoose.Schema
const BookSchema = new schema({
title: String,
author: String,
isbn: Number,
date: { type: Date, default: Date.now},
description: String
})
module.exports = mongoose.model('Book', BookSchema)
这是我的书籍模型控制器
and here is my controller for the book model
const Book = require('../models/book')
const express = require('express')
router = express.Router()
router.route('/books')
// Create a book
.post( (req, res) => {
const book = new Book()
book.name = req.body.name
book.save( (err) => {
if (err)
res.send(err)
console.log('Book created! ')
})
})
//get all books
.get( (req, res) => {
Book.find( (err, books) => {
if (err)
res.send(err)
res.render('books', {title: 'books list'})
})
})
module.exports = router
最后是我的翡翠模板
extends layout
block content
if books
each book in books
h1 #{book.title}
推荐答案
您的代码中需要进行多个错误/修改.
There are multiple mistakes/modifications required in your code.
-
在查找时,最好以
{}
作为第一个输入.
在渲染书籍模板时,您使用books
变量显示书籍列表,但不是从路径发送它.您需要在res.render
中发送books
.
When rendering the book template, you are using books
variable to show list of books, but you are not sending it from the route. you need to send books
in res.render
.
尝试一下:
router.route('/books')
// Create a book
.post( (req, res) => {
const book = new Book()
book.name = req.body.name
book.save( (err) => {
res.send(err)
console.log('Book created! ')
})
})
//get all books
.get((req, res) => {
Book.find({},(err, books) => {
if (err)
res.send(err)
res.render('books', {title: 'books list' , books : books})//need to send the books variable to the template.
})
})
这篇关于试图从mongodb中显示玉器中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文