Javascript 引用的对象和虚拟getter在pug布局文件中返回未定义,但已从数据库中成功查询

Javascript 引用的对象和虚拟getter在pug布局文件中返回未定义,但已从数据库中成功查询,javascript,node.js,express,pug,Javascript,Node.js,Express,Pug,我的问题有两个方面,但我认为它们都是由同一个问题引起的:我的模式定义和/或模型创建 我在跟踪,我想不出: 为什么我的引用对象在布局文件中返回未定义,但我已确认数据已正确插入和查询(据我所知) 我的虚拟id获取程序不工作 架构定义: function authorCreate(first_name, family_name, d_birth, d_death, cb) { authordetail = {first_name:first_name , family_name: family_n

我的问题有两个方面,但我认为它们都是由同一个问题引起的:我的模式定义和/或模型创建

我在跟踪,我想不出:

  • 为什么我的引用对象在布局文件中返回未定义,但我已确认数据已正确插入和查询(据我所知)

  • 我的虚拟id获取程序不工作

  • 架构定义:

    function authorCreate(first_name, family_name, d_birth, d_death, cb) {
      authordetail = {first_name:first_name , family_name: family_name }
      if (d_birth != false) authordetail.date_of_birth = d_birth
      if (d_death != false) authordetail.date_of_death = d_death
    
      var author = new Author(authordetail);
    
      author.save(function (err) {
        if (err) {
          cb(err, null)
          return
        }
        console.log('New Author: ' + author);
        authors.push(author._id)
        cb(null, author)
      }  );
    }
    
    function bookCreate(title, summary, isbn, author, genre, cb) {
      bookdetail = {
        title: title,
        summary: summary,
        author: author,
        isbn: isbn
      }
      if (genre != false) bookdetail.genre = genre
    
      var book = new Book(bookdetail);
      book.save(function (err) {
        if (err) {
          cb(err, null)
          return
        }
        console.log('New Book: ' + book);
        books.push(book._id)
        cb(null, book)
      }  );
    }
    
    // Display list of all books.
    exports.book_list = function(req, res) {
    
      Book.find({})
      .populate('author')//this seems to work
      .exec(function (err, list_books) {
        if (err) { return next(err); }
        //Successful, so render
        for(var b in list_books){
          console.log('author id: ' + list_books[b].author._id);//as evidenced here
    
        }
    
        //but the author object is undefined when rendered
        res.render('book_list', { title: 'Book List', book_list: list_books });
    
      });
    
    };
    
    book.js斜体

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    
    const BookSchema = new Schema({
    
      title: {type: String, required: true},
      author: {type: Schema.Types.ObjectId, ref: 'Author', required: true},
      summary: {type: String, required: true},
      isbn: {type:String, required: true},
      genre: [{type: Schema.Types.ObjectId, ref: 'Genre'}]
    
    });
    
    //this doesnt seem to be working. returns undefined on the
    //object in layout file
    BookSchema
    .virtual('url')
    .get(() => {
      return '/catalog/book/' + this._id;
    });
    
    module.exports = mongoose.model('Book', BookSchema);
    
    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    
    const AuthorSchema = new Schema({
    
      first_name: {type: String, required: true, max: 100},
      family_name: {type: String, required: true, max: 100},
      date_of_birth: {type: Date},
      date_of_death: {type: Date}
    
    });
    
    //Virtuals
    
    AuthorSchema.virtual('name')
    .get(() => {
      return this.family_name + ', ' + this.first_name;
    });
    
    //create virtual absolute url to obtain instance of this model
    AuthorSchema.virtual('url')
    .get(() => {
      return '/catalog/author/' + this._id;
    });
    
    module.exports = mongoose.model('Author', AuthorSchema);
    
    author.js斜体

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    
    const BookSchema = new Schema({
    
      title: {type: String, required: true},
      author: {type: Schema.Types.ObjectId, ref: 'Author', required: true},
      summary: {type: String, required: true},
      isbn: {type:String, required: true},
      genre: [{type: Schema.Types.ObjectId, ref: 'Genre'}]
    
    });
    
    //this doesnt seem to be working. returns undefined on the
    //object in layout file
    BookSchema
    .virtual('url')
    .get(() => {
      return '/catalog/book/' + this._id;
    });
    
    module.exports = mongoose.model('Book', BookSchema);
    
    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    
    const AuthorSchema = new Schema({
    
      first_name: {type: String, required: true, max: 100},
      family_name: {type: String, required: true, max: 100},
      date_of_birth: {type: Date},
      date_of_death: {type: Date}
    
    });
    
    //Virtuals
    
    AuthorSchema.virtual('name')
    .get(() => {
      return this.family_name + ', ' + this.first_name;
    });
    
    //create virtual absolute url to obtain instance of this model
    AuthorSchema.virtual('url')
    .get(() => {
      return '/catalog/author/' + this._id;
    });
    
    module.exports = mongoose.model('Author', AuthorSchema);
    
    模型创建:

    function authorCreate(first_name, family_name, d_birth, d_death, cb) {
      authordetail = {first_name:first_name , family_name: family_name }
      if (d_birth != false) authordetail.date_of_birth = d_birth
      if (d_death != false) authordetail.date_of_death = d_death
    
      var author = new Author(authordetail);
    
      author.save(function (err) {
        if (err) {
          cb(err, null)
          return
        }
        console.log('New Author: ' + author);
        authors.push(author._id)
        cb(null, author)
      }  );
    }
    
    function bookCreate(title, summary, isbn, author, genre, cb) {
      bookdetail = {
        title: title,
        summary: summary,
        author: author,
        isbn: isbn
      }
      if (genre != false) bookdetail.genre = genre
    
      var book = new Book(bookdetail);
      book.save(function (err) {
        if (err) {
          cb(err, null)
          return
        }
        console.log('New Book: ' + book);
        books.push(book._id)
        cb(null, book)
      }  );
    }
    
    // Display list of all books.
    exports.book_list = function(req, res) {
    
      Book.find({})
      .populate('author')//this seems to work
      .exec(function (err, list_books) {
        if (err) { return next(err); }
        //Successful, so render
        for(var b in list_books){
          console.log('author id: ' + list_books[b].author._id);//as evidenced here
    
        }
    
        //but the author object is undefined when rendered
        res.render('book_list', { title: 'Book List', book_list: list_books });
    
      });
    
    };
    
    controller.js:

    function authorCreate(first_name, family_name, d_birth, d_death, cb) {
      authordetail = {first_name:first_name , family_name: family_name }
      if (d_birth != false) authordetail.date_of_birth = d_birth
      if (d_death != false) authordetail.date_of_death = d_death
    
      var author = new Author(authordetail);
    
      author.save(function (err) {
        if (err) {
          cb(err, null)
          return
        }
        console.log('New Author: ' + author);
        authors.push(author._id)
        cb(null, author)
      }  );
    }
    
    function bookCreate(title, summary, isbn, author, genre, cb) {
      bookdetail = {
        title: title,
        summary: summary,
        author: author,
        isbn: isbn
      }
      if (genre != false) bookdetail.genre = genre
    
      var book = new Book(bookdetail);
      book.save(function (err) {
        if (err) {
          cb(err, null)
          return
        }
        console.log('New Book: ' + book);
        books.push(book._id)
        cb(null, book)
      }  );
    }
    
    // Display list of all books.
    exports.book_list = function(req, res) {
    
      Book.find({})
      .populate('author')//this seems to work
      .exec(function (err, list_books) {
        if (err) { return next(err); }
        //Successful, so render
        for(var b in list_books){
          console.log('author id: ' + list_books[b].author._id);//as evidenced here
    
        }
    
        //but the author object is undefined when rendered
        res.render('book_list', { title: 'Book List', book_list: list_books });
    
      });
    
    };
    
    布局。帕格狗

    extends layout
    
    block content
      h1= title
    //url and author undefined here
      ul
        each book in book_list
          li
            a(href=book.url) #{book.title}
            |  #{book.author.name}
    
        else
          li There are no books.
    
    屏幕截图:

    function authorCreate(first_name, family_name, d_birth, d_death, cb) {
      authordetail = {first_name:first_name , family_name: family_name }
      if (d_birth != false) authordetail.date_of_birth = d_birth
      if (d_death != false) authordetail.date_of_death = d_death
    
      var author = new Author(authordetail);
    
      author.save(function (err) {
        if (err) {
          cb(err, null)
          return
        }
        console.log('New Author: ' + author);
        authors.push(author._id)
        cb(null, author)
      }  );
    }
    
    function bookCreate(title, summary, isbn, author, genre, cb) {
      bookdetail = {
        title: title,
        summary: summary,
        author: author,
        isbn: isbn
      }
      if (genre != false) bookdetail.genre = genre
    
      var book = new Book(bookdetail);
      book.save(function (err) {
        if (err) {
          cb(err, null)
          return
        }
        console.log('New Book: ' + book);
        books.push(book._id)
        cb(null, book)
      }  );
    }
    
    // Display list of all books.
    exports.book_list = function(req, res) {
    
      Book.find({})
      .populate('author')//this seems to work
      .exec(function (err, list_books) {
        if (err) { return next(err); }
        //Successful, so render
        for(var b in list_books){
          console.log('author id: ' + list_books[b].author._id);//as evidenced here
    
        }
    
        //but the author object is undefined when rendered
        res.render('book_list', { title: 'Book List', book_list: list_books });
    
      });
    
    };
    

    工具:

    • Node+Express-js

    • 帕格

    • 自由层

    我对Mongoose DB&Pug是一个全新的人,我所知道的知识和教程中的入门知识一样多,并明确指出需要进一步阅读


    如果你需要更多的信息,请告诉我。谢谢

    看来我的问题是胖箭头函数的使用。将这些更改为常规
    function()
    语法解决了此问题

    确认这是nodejs中的一个支持问题