복붙노트

[MONGODB] 몽구스 스키마 참조 및 정의되지 않은 형식 'OBJECTID'

MONGODB

몽구스 스키마 참조 및 정의되지 않은 형식 'OBJECTID'

내 스키마 사이에 어떤 관계를 할 노력하고있어 나는 내 솔루션 몇 가지 문제가있다. 여기 내 장치 스키마는 다음과 같습니다

var deviceSchema = schema({
    name : String,
    type : String,
    room: {type: mongoose.Types.ObjectId,  ref: 'Room'},
    users: [{type:mongoose.Types.ObjectId, ref: 'User'}]
});

여기 방 스키마 :

var roomSchema = schema({
    name : String,
    image : String,
    devices: [{type: mongoose.Types.ObjectId, ref: 'Device'}]
});

몽구스는 오류가 발생합니다

만약 내가 변화 룸 : {유형 : mongoose.Types.ObjectId, REF : '룸'}, 방 {유형 : 숫자, REF : '룸'}, 모든 작동합니다. 당신은 이런 일이 왜 나를 설명 할 수 있을까요?

해결법

  1. ==============================

    1.mongoose.Types.ObjectId가 ObjectId가 생성자 함수입니다, 당신이 스키마 정의에서 사용하고자하는 mongoose.Schema.Types.ObjectId (또는 mongoose.Schema.ObjectId)입니다.

    mongoose.Types.ObjectId가 ObjectId가 생성자 함수입니다, 당신이 스키마 정의에서 사용하고자하는 mongoose.Schema.Types.ObjectId (또는 mongoose.Schema.ObjectId)입니다.

    그래서 deviceSchema이 대신과 같아야합니다 :

    var deviceSchema = schema({
        name : String,
        type : String,
        room: {type: mongoose.Schema.Types.ObjectId,  ref: 'Room'},
        users: [{type:mongoose.Schema.Types.ObjectId, ref: 'User'}]
    });
    
  2. from https://stackoverflow.com/questions/28617798/mongoose-schema-reference-and-undefined-type-objectid by cc-by-sa and MIT license