mongodb 缺少架构错误:尚未为模型注册架构无法找到解析

ctzwtxfj  于 2022-12-03  发布在  Go
关注(0)|答案(1)|浏览(120)

我尝试在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

改了型号名字也没关系,总是同样的错误,不知道哪里做错了,已经做了好几个小时了,真的让我很沮丧,有没有人遇到过这种情况?

ffscu2ro

ffscu2ro1#

尝试使用您在collection中提到的实际集合名称

const TradeSchema = new mongoose.Schema({
   
    useradmin:{
        type: String,
        required: [true, 'usuario requerido']
    },
    type:{
        type: mongoose.Types.ObjectId,
        ref: "tradeType" // CHANGE THE ref NAME
    },
    status:{
        type: Boolean,
        required:false,
        default: true,
    }
},
{ collection: 'trade', timestamps: true, versionKey: 'version' })

尝试使用实际名称,还有一件事,您是否在上面的查询中获得了没有交易类型的交易对象?

相关问题