Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将数据从路由传递到pug文件_Javascript_Node.js_Sequelize.js_Pug - Fatal编程技术网

Javascript 如何将数据从路由传递到pug文件

Javascript 如何将数据从路由传递到pug文件,javascript,node.js,sequelize.js,pug,Javascript,Node.js,Sequelize.js,Pug,我有一个名为books.js的pug文件,其中我基于:id呈现一个表单,并使用findById()在sequelize中从模型传入的值填充表单中的文本字段 books.js router.get('/:id', function(req, res, next) { Book.findById(req.params.id).then(function(book){ res.render('update-book.pug', {book: book, title: "Update Boo

我有一个名为books.js的pug文件,其中我基于:id呈现一个表单,并使用findById()在sequelize中从模型传入的值填充表单中的文本字段

books.js

router.get('/:id', function(req, res, next) {
  Book.findById(req.params.id).then(function(book){
    res.render('update-book.pug', {book: book, title: "Update Book"}) 
    console.log(book)
  })
 });
这是我试图使用根据id确定的值来呈现的pug文件,我在值之后使用单词book.,因为我的理解是books.js中的函数参数
book
应该包含数据

extends layout.pug

block content
  h1= title

    form

      p
        label(for='title') Title
        input#title(name='title', type='text', value=book.title)
      p
        label(for='author') Author
        input#author(name='author', type='text', value=book.author)
      p
        label(for='genre') Genre
        input#genre(name='genre', type='text', value=book.genre)
      p
        label(for='year') Year
        input#year(name='year', type='text', value=book.year)
      p
        input(type='submit', value='Update Book')
    form(method='post', action='/books/8/delete', onsubmit="return confirm('Do you really want to delete this book?');")
      p
        a.button(href='/') Cancel
      p
        input(type='submit', value='Delete Book')
在我的终端,我收到了这个消息

sequelize已弃用的Model.findById已弃用,请改用Model.findByPk节点_modules/sequelize/lib/Model.js:4208:9

在我的浏览器中,我发现以下错误:


有人能帮忙吗?

你试过改用findByPk吗?这是做什么的?您需要对JSON.decode(JSON.encode(data))进行解码,然后将结果传递到视图中。您还可以使用Book.findOne({where:{id:req.params.id}}),这将只给出一个结果。我尝试使用findByPk,但也不起作用,这意味着您的
Book
变量返回为
null
。因此,您的
Book.findById
返回null。这可能是因为在数据库中找不到任何具有指定id的匹配图书,也可能是因为此函数已被弃用。当然,如果不推荐使用替代解决方案,您应该使用它。