我尝试在mongo中进行填充,我尝试了所有方法,但总是出现此错误。我已经很沮丧了
mi函数
const trade = Trade.find({ useradmin: idUser }).populate('type')
示范贸易
import mongoose from "mongoose"
export interface iTrade extends mongoose.Document{
useradmin : string,
type : string,
status : boolean
}
const TradeSchema = new mongoose.Schema({
useradmin:{
type: String,
required: [true, 'usuario requerido']
},
type:{
type: mongoose.Types.ObjectId,
ref: "TradeType"
},
status:{
type: Boolean,
required:false,
default: true,
}
},
{ collection: 'trade', timestamps: true, versionKey: 'version' })
TradeSchema.methods.toJSON = function () {
const {__v,version, ... data } = this.toObject();
return data;
}
const Trade = mongoose.model<iTrade>("trade", TradeSchema)
export default Trade
模型贸易类型
import mongoose from "mongoose"
export interface iTradeType extends mongoose.Document{
name : string
}
const TradeTypeSchema = new mongoose.Schema({
name: {
type: String
}
},
{ collection: 'tradeType', timestamps: true, versionKey: 'version' })
TradeTypeSchema.methods.toJSON = function () {
const {__v,version, ... data } = this.toObject();
return data;
}
const TradeType = mongoose.model<iTradeType>("TradeType", TradeTypeSchema)
export default TradeType
改了型号名字也没关系,总是同样的错误,不知道哪里做错了,已经做了好几个小时了,真的让我很沮丧,有没有人遇到过这种情况?
1条答案
按热度按时间ffscu2ro1#
尝试使用您在collection中提到的实际集合名称
尝试使用实际名称,还有一件事,您是否在上面的查询中获得了没有交易类型的交易对象?