【发布时间】:2019-01-07 20:49:21
【问题描述】:
我在节点 js 中有以下模型,我想在一次调用中从文件架构和客户端架构中获取数据,我正在阅读有关填充但不知道如何使用它。
这是我的模特
const mongoose = require('mongoose');
const fileSchema = mongoose.Schema({
_id: mongoose.SchemaTypes.ObjectId,
client_id: mongoose.SchemaTypes.ObjectId,
user_id: mongoose.SchemaTypes.ObjectId,
status: String,
name: String,
path: String,
clients: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Client' }]
});
const clientSchema = mongoose.Schema({
_id: mongoose.SchemaTypes.ObjectId,
name: String,
img: String
});
module.exports =
mongoose.model('File', fileSchema, 'files'),
Client = mongoose.model('Client', clientSchema, 'clientes');
这就是我现在获取文件数据的方式
exports.getFiles = (req, res, next) => {
File.find({ field: res.locals.field })
.select('_id client_id user_id status name path')
.exec()
.then(file => {
res.status(200).json({
response: file
});
})
.catch(err => {
console.log(err);
res.status('500').json({
error: err
});
});
};
这会返回一个 json 响应,当我尝试使用填充时,我得到了一个空数组。
【问题讨论】:
标签: node.js mongoose mongoose-populate