gpt4 book ai didi

javascript - 在 pug 布局文件中返回 undefined reference 对象和虚拟 getter,但从数据库中成功查询

转载 作者:行者123 更新时间:2023-11-30 20:14:47 25 4
gpt4 key购买 nike

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

我正在关注 this MDN Nodejs tutorial我想不通:

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

  2. 我的虚拟 id getter 不工作。

架构定义:

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

author.js 斜体

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)
} );
}

controller.js:

// 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 });

});

};

layout.pug:

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.

截图:

This is the end result

工具:

  • Node + Express js

  • 哈巴狗

  • mLab 免费套餐

我对 Mongoose DB 和 Pug 完全陌生,只知道教程中的入门书所教授的内容,并明确指出作为进一步阅读的内容。

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

最佳答案

我的问题似乎是粗箭头函数的使用。将这些更改为常规 function() 语法解决了这个问题。

This answer确认它是 nodejs 中的支持问题。

关于javascript - 在 pug 布局文件中返回 undefined reference 对象和虚拟 getter,但从数据库中成功查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52029229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com