问题描述
我正在使用带有节点和快递的猫鼬。在 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 (将#修改为@)