mongodb - mongoose referencing not working when object_id is string -


i have 2 mongo collections, 1 department , 1 users

    var deptschema = new schema({ name: {     type: string,     required: 'department name missing.' }},{ timestamps: true }); deptschema.plugin(sequencegenerator, { field: '_id', startat: '0001', prefix: 'dept' }); module.exports = mongoose.model('departments', deptschema);  //user or staff schema definition var userschema = new schema({ name: {     type: string,     required: 'user name missing.' }, role: {     type: string,     required: 'role missing.',     enum: ['admin', 'sales'] }, passwordhash: string, passwordsalt: string, departmentid: {     type: schema.types.objectid,     required: 'department id missing.',     ref: 'departments' } },{ timestamps: true }); userschema.plugin(sequencegenerator, { field: '_id', startat: '0001', prefix: 'staff' }); module.exports = mongoose.model('users', userschema); 

now have inserted 1 document in departments collection _id 'dept0001' while registering user data

    { name: 'rahul agarwal',       username: 'rahul43',       password: 'rev@888',       departmentid: 'dept0001',       role: 'admin' } 

on postman gives error

    { "errors": {     "departmentid": {         "message": "cast objectid failed value \"dept0001\" @ path \"departmentid\"",         "name": "casterror",         "stringvalue": "\"dept0001\"",         "kind": "objectid",         "value": "dept0001",         "path": "departmentid",         "reason": {             "message": "cast objectid failed value \"dept0001\" @ path \"departmentid\"",             "name": "casterror",             "stringvalue": "\"dept0001\"",             "kind": "objectid",             "value": "dept0001",             "path": "departmentid"         }     } }, "_message": "users validation failed", "message": "users validation failed: departmentid: cast objectid failed value \"dept0001\" @ path \"departmentid\"", "name": "validationerror"} 

my question should not use customized _id field reference, if feasible, why can't reference it.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -