第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

開心每一天1111 2021-12-02 19:41:51
我正在嘗試顯示數(shù)據(jù)庫中的數(shù)據(jù)。我有 3 個(gè)架構(gòu),將所有架構(gòu)合二為一。但是,組合數(shù)據(jù)沒有顯示。我附上了我的 3 架構(gòu)。async-wait 與 try-catch 一起工作正常,這對我來說似乎很干凈。我也嘗試關(guān)注mongoose populate。兩者都返回相同的結(jié)果。需要說明的是:我是新手。因此,不要對要遵循的最佳實(shí)踐有很好的了解。書籍架構(gòu):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);作者架構(gòu):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);流派架構(gòu):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作者數(shù)據(jù)未顯示如果我使用 .populate(),那么。它正在顯示(南)如果我不使用填充,它不會(huì)返回任何內(nèi)容預(yù)期輸出: 猿和天使(約翰)當(dāng)前輸出: 猿和天使(NaN)
查看完整描述

2 回答

?
拉丁的傳說

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊

請嘗試以下代碼。我認(rèn)為,它會(huì)起作用


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

    }

};


查看完整回答
反對 回復(fù) 2021-12-02
?
當(dāng)年話下

TA貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊

在我的查詢中,我只需要添加回調(diào),如:


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 中的箭頭函數(shù)。我使用箭頭函數(shù)來獲取對象。但是,箭頭函數(shù)不適用于對象。

查看完整回答
反對 回復(fù) 2021-12-02
  • 2 回答
  • 0 關(guān)注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)