猫鼬填充显示模式尚未注册为模型填充 [英] mongoose populate shows schema hasn't been registered for model populate
本文介绍了猫鼬填充显示模式尚未注册为模型填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
创建了称为电影的猫鼬模式,该对象具有对象属性,该对象引用了由multer-gridfs存储创建的另一个集合,但是当调用填充方法时,文件ID上为空
created mongoose schema called movie with property of object reference to another collection of created by multer-gridfs storage but when called populate method i got null on fileID
架构为
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const MovieSchema = new Schema({
description: String,
category: String,
token: String,
fileID: {
type: mongoose.Schema.Types.ObjectId,
ref: 'fs' //created by multer gridfs storage
}
});
const Movie = mongoose.model('Movies', MovieSchema);
module.exports = Movie;
const express = require('express');
const multer = require('multer');
const path = require('path')
const router = express.Router();
const Movie = require('../models/Movie');
const GridFsStorage = require('multer-gridfs-storage');
const storage = new GridFsStorage({
url: 'mongodb://127.0.0.1:27017/kannywoodtv-dev',
file: (req, file) => {
return {
filename: req.body.name + path.extname(file.originalname)
};
}
});
const upload = multer({
storage
});
router.post('/', upload.single('file'), (req, res) => {
console.log(req.file)
const movie = new Movie({
description: req.body.Description,
category: req.body.Category,
token: req.body.Description,
fileID: req.file.id
});
console.log(movie)
movie.save(function(err) {
if (err) {
console.log(err);
return;
}
res.json({
"success": "true"
});
});
});
router.get('/movies', (req, res) => {
Movie.find()
.populate('fileID')
.then(files => {
res.send(files)
}).catch(err => {
res.send(err)
})
})
module.exports = router;
当我调用填充时,fileID返回null,但如果删除填充,则返回我引用的字段的objectID
fileID return when null when i called populate but return the objectsID of the field i reference if i remove populate
推荐答案
将此行添加到movieSchema中
add this line in your movieSchema
const Movies = mongoose.model('Movies', MovieSchema);
module.exports = { Movies };
并将其导入为
const { Movies } = require('../models/Movie');
这篇关于猫鼬填充显示模式尚未注册为模型填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文