2021年最后一天!在花了 5 個多小時在互聯網上搜索后,我求助于發帖尋求幫助。
期望的行為:
- 路由回傳的檔案串列將包含填充的檔案,而不僅僅是參考 ID
嘗試的解決方案:
- 填充后添加 exec 函式
- Stackoverflow 使我陷入了認為該問題與 mongoose 連接有關的兔子洞,因此我修改了后端,以便我的模型檔案回傳模式而不是模型,然后使用 mongoose.createConnection 而不是 .connect,并創建了一個資料庫。 js 模塊來保存每個模型的單個實體 (connection.mode()),然后路由檔案可以訪問和共享。我沒有達到可以測驗 .populate 的程度,所以我 git reset 回到下面的代碼
錯誤
MissingSchemaError: Schema hasn't been registered for model "progressions".
Use mongoose.model(name, schema)
請注意,嘗試填充“課程”時也會引發此錯誤
進度模型
const Progress = mongoose.model('Progress', new Schema({
lesson: {
type: Schema.Types.ObjectId,
ref: 'lesson'
},
progressions: [{
type: Schema.Types.ObjectId,
ref: 'progression'
}],
progress:{
type: Number
},
user:{
type: Schema.Types.ObjectId,
ref: 'user'
}
},{
timestamps: true,
}));
module.exports = Progress;
進度路線檔案
router.get('/all', async (req, res) => {
let user = await User.find({email:req.query.user})
let data = await Progress.find({user:user}).populate('progressions')
return res.status(201).json({
data,
message: 'data populated',
success: true
});
});
進步模型
const Progression = mongoose.model('Progression', new Schema({
type:{
type:String
},
toBeReviewed:{
type: Date,
default: Date.now
},
},{
timestamps: true,
}));
module.exports = Progression;
uj5u.com熱心網友回復:
貓鼬檔案中的所有代碼在用于其他檔案之前應該已經運行。
發生此錯誤的原因可能是您的貓鼬模型在用于其他檔案之前未執行。
uj5u.com熱心網友回復:
您是否嘗試在將“Progressions”傳遞給 populate 時使用它?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399219.html
下一篇:MongoDB中的默認值
