类型错误:stori.populate() 不是函数

问题描述

我正在使用带有节点和快递的猫鼬。在 mongoose 的官方文档中有一个叫做 populate 的特性,它看起来非常方便在 mongoose 文档中引用和使用其他文档。我试图通过向“/check_populate”发送获取请求来检查文档是否已填充 但我收到这个 typeError 说 populate 不是一个函数。

 const express = require("express");
    const app = express();
    const mongoose = require("mongoose");
    
    const person_schema = new mongoose.Schema({
        name : String,age : Number,stories : [{type : mongoose.Schema.Types.ObjectId,ref : "story"}]
    })
    
    const story_schema = new mongoose.Schema({
        title : String,author : {type : mongoose.Schema.Types.ObjectId,ref : 'perso'},date : String,followers : [{type : mongoose.Schema.Types.ObjectId,ref : 'perso'}]
    })
    
    const person = mongoose.model("perso",person_schema);
    const story = mongoose.model("story",story_schema);
    
    const dbURI = "mongodb+srv://shyam:passw0rd@cluster0.7jmb0.mongodb.net/node-tut?retryWrites=true&w=majority"
    mongoose.connect(dbURI)
      .then(console.log("connected to db"),app.listen(3000));
    
    
    const shyam = new person({
        // _id : new Schema._id,name : "Shyam",age : 18,// stories : [story1._id,story2._id]
    })  
    
    
    const story1 = new story({
        //  _id : new Schema._id,title : "thirsty crow",author : shyam._id,date : new Date()
    })
    
    
    const story2 = new story({
        // _id : new Schema._id,title : "rat and the lion",date : new Date()
    })
    
    // const ram = new person({
       // _id : new Schema._id,//     name : "Ram",//     age : 18,//     stories : []
    // })  
    app.get("/",(req,res)=>{
        try{
        story1.save();
        story2.save();
        shyam.save();
    }
    catch{
        console.log(err);
    }
    })
    
    app.get("/check_populate",res)=>{
        const stori = story.findOne({title : "rat and the lion"})
        console.log(stori.populated('author'));
    })

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...