亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Node.js:貓鼬 .populate() 不顯示數據

Node.js:貓鼬 .populate() 不顯示數據

開心每一天1111 2021-12-02 19:41:51
我正在嘗試顯示數據庫中的數據。我有 3 個架構,將所有架構合二為一。但是,組合數據沒有顯示。我附上了我的 3 架構。async-wait 與 try-catch 一起工作正常,這對我來說似乎很干凈。我也嘗試關注mongoose populate。兩者都返回相同的結果。需要說明的是:我是新手。因此,不要對要遵循的最佳實踐有很好的了解。書籍架構:const mongoose = require('mongoose');const Schema   = mongoose.Schema;const BookSchema = new Schema({    title: {        type     : String,        required : [true, 'Book Title is Required'],        max      : 100,        min      : 5,        trim     : true,        lowercase: true    },    author: {        type    : Schema.Types.ObjectId,        ref     : 'Author',        required: [true, 'Author is Required']    }    genre: [{        type: Schema.Types.ObjectId,        ref : 'Genre'    }]}, { collection : 'book', timestamps: true });BookSchema.virtual('url').get(() => {    return 'book/' + this._id;});module.exports = mongoose.model('Book', BookSchema);作者架構:const mongoose = require('mongoose');const Schema   = mongoose.Schema;const AuthorSchema = new Schema({    firstName: {        type     : String,        required : [true, 'First Name is Required'],        max      : 100,        min      : 5,        trim     : true,        lowercase: true    },    lastName: {        type     : String,        required : [true, 'Last Name is Required'],        max      : 100,        min      : 5,        trim     : true,        lowercase: true    }}, { collection : 'author', timestamps: true });AuthorSchema.virtual('name').get(() => {    return this.firstName + this.lastName;});module.exports = mongoose.model('Author', AuthorSchema);流派架構:const mongoose = require('mongoose');const Schema   = mongoose.Schema;const GenreSchema = new Schema({    name: {        type     : String,        required : [true, 'Genre Name is Required'],        max      : 100,        min      : 3,        trim     : true,        lowercase: true    }}, { collection : 'genre', timestamps: true });module.exports = mongoose.model('Genre', GenreSchema);URL 未附加 ID作者數據未顯示如果我使用 .populate(),那么。它正在顯示(南)如果我不使用填充,它不會返回任何內容預期輸出: 猿和天使(約翰)當前輸出: 猿和天使(NaN)
查看完整描述

2 回答

?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

請嘗試以下代碼。我認為,它會起作用


exports.bookList = async(req, res, next) => {

    try {

        const bookList = await Book.find({}).populate('author').exec((error, list) => list);


        res.render('./book/index', { title: 'Book List', bookList: bookList});

    } catch (error) {

        res.status(500).json({ message: error.message });

    }

};


查看完整回答
反對 回復 2021-12-02
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

在我的查詢中,我只需要添加回調,如:


exports.bookList = async(req, res, next) => {

    try {

        const bookList = await Book.find({}).populate('author').exec((err, bookList) => {

            if (err) return bookInstanceList;


            // console.log(bookList);


            res.render('./book/index', { title: 'Book List', bookList: bookList});

        });


    } catch (error) {

        res.status(500).json({ message: error.message });

    }

};

主要問題是 Schema 中的箭頭函數。我使用箭頭函數來獲取對象。但是,箭頭函數不適用于對象。

查看完整回答
反對 回復 2021-12-02
  • 2 回答
  • 0 關注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號