Node.js 填充导致结果丢失的字段
我正在使用express和mongo在Node中制作一个应用程序。在尝试填充查询结果时,当我尝试填充特定字段时,结果会丢失,请您指出此处的错误 project.ts文件Node.js 填充导致结果丢失的字段,node.js,mongoose,schema,mongoose-schema,mongoose-populate,Node.js,Mongoose,Schema,Mongoose Schema,Mongoose Populate,我正在使用express和mongo在Node中制作一个应用程序。在尝试填充查询结果时,当我尝试填充特定字段时,结果会丢失,请您指出此处的错误 project.ts文件 import mongoose, { Schema, Document } from 'mongoose'; import { IUser } from './user'; import { IFiles } from './file' import { ICategory } from './category' import
import mongoose, { Schema, Document } from 'mongoose';
import { IUser } from './user';
import { IFiles } from './file'
import { ICategory } from './category'
import { ITags } from './tags'
export interface featured_image {
data: Buffer;
contentType: String;
}
export interface IProject extends Document {
...
user: IUser['_id'];
files: Array< IFiles['_id'] >;
tags: Array< ITags['_id'] >;
category: ICategory['_id'];
.....
}
const ProjectSchema: Schema = new Schema({
....
user: {
type: Schema.Types.ObjectId,
ref: "User"
},
files: [{
type: Schema.Types.ObjectId,
ref: "File"
}],
category :{
type: Schema.Types.ObjectId,
ref: "Category"
},
tags : [{
type: Schema.Types.ObjectId,
ref: "Tags"
}]
.........
},
{ timestamps: true }
);
export default mongoose.model<IProject>('Project', ProjectSchema);
它返回:
{
"error": "Project not found"
}
如果我只是删除.populate('tags')
,我会得到相同项目的实际结果
注意:当我测试这个时,标签数组是空的
import mongoose, { Schema, Document } from 'mongoose';
import { ICategory } from './category';
export interface ITags extends Document {
name: string;
type: string;
count:number;
category:ICategory['_id'];
}
const TagsSchema: Schema = new Schema({
name: {
type: String,
unique:true,
lowercase:true
},
type:{
type:string, //project category | profile | project tag
},
count: Number,
category: {
type: Schema.Types.ObjectId,
ref: "Category"
}
},
{ timestamps: true }
);
export default mongoose.model<ITags>('Tags', TagsSchema);
{
"error": "Project not found"
}