簡體   English   中英

我不能應付貓鼬的人口

[英]I can't handle populate in mongoose

我認為在控制台中嘗試登錄“ category”時,並沒有填充類別,因為我收到“ ReferenceError:類別未定義”。 對我來說,就像在文檔中一樣,但正如我們所看到的那樣。 有誰能告訴我這是怎么回事?

//model/Category.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
 const CatSchema = new Schema({
    title: {
        type: String,
        required: true
    },
    body: {
        type: String,
        required: true
    }
});
 mongoose.model("categories", CatSchema, "categories");

模型/ Story.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
     const StorySchema = new Schema({
    title: {
        type: String,
        required: true
    },
    body: {
        type: String,
        required: true
    },
    category: {
        type: Schema.Types.ObjectId,
        ref: "categories"
    }

});
     mongoose.model("stories", StorySchema, "stories");

路線/ stories.js

const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Category = mongoose.model('categories');
const Story = mongoose.model('stories');
     router.get('/add', (req, res) => {
    Story.find()
        .populate('category',  'title')
        .then(stories => {
            res.render('stories/add', {
                stories: stories
            });
        });
});

Query.prototype.populate()返回一個需要運行.exec()Query對象,請嘗試以下操作:

Story.find({})
    .populate('category',  'title')
    .exec()
    .then(stories => {
        res.render('stories/add', {
            stories: stories
        });
});

我要如何使用它存在問題。 代碼有效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM