mongodb Mongoose从模式类型为对象ID的值返回空值

vcirk6k6  于 2023-01-04  发布在  Go
关注(0)|答案(1)|浏览(146)

值为“base_id”且类型为www.example.com的简单mongoose架构Object.id

const mongoose = require ('mongoose');

const Schema = mongoose.Schema;

const qrSchema = new Schema({
    token: {
        type: String,
        unique: true,
        required: true
    },
    base_id : {        
        type: mongoose.Schema.Types.ObjectId, ref: 'Base'
    },    
    // etc
});

const QR = mongoose.model('QR', qrSchema);
module.exports = QR;

引用'Base'指的是另一个模型。我想提取一个填充的结果。但即使是没有填充的普通请求:

const user = await QRToken.findOne({token: req.query.token})    

console.log(user)

在响应中,console.log中的“base_id”返回空值:

{
  _id: new ObjectId("631230020d0572fac188dc1"),      
  token: '9bb8329611qab805f4bcc7f3066c94cbbd0d430f8fcf4cea09bdf191ef89887',
  base_id: null,
   //etc      
}
//base Schema
const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const baseSchema = new Schema({
    firstname: {
        type: String,
        required: true
    },
    lastname: {
        type: String,
        required: true
    },
    userName: { //mailaddress
        type: String,
        unique: true,
        trim: true,
        lowercase: true
    },
    telephone: {
        type: String,
        unique: true,
        trim: true
    },
    password: {
        type: String,
        required: true,
        select: false
    },

    token: {
        type: String
    }
}, options);

const Base = mongoose.model('Base', baseSchema);
module.exports = Base;

我哪里做错了?

vhmi4jdf

vhmi4jdf1#

我为什么会有这个错误。lpizzinidev触发我检查基本模型。我使用这个模型,填充等,现在2年的试点。从来没有一个错误。也许与更新的东西改变了它是大小写敏感。

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

//discriminatorKey
const options = {
    discriminatorKey: 'kind',
    collection: 'base'
};

//change the collectionname from 'Base' to 'base' did the job.

相关问题