Reputation: 99960
I have this query:
return Role.findById(_id, {lean: true}).exec().then(function (role: any) {
if (!(role && role.key && role._id)) {
log.error('role doc was missing the "key"/"_id" field =>', role);
return;
}
caches.RolesCache[role.key] = role;
caches.RolesCacheById[role._id] = role;
});
I am getting this logged:
role doc was missing the "key"/"_id" field => { _id: 5ade466c1384a4deffac6b09 }
Why would the findById function only return the _id field of the document? So weird. I swear this used to work fine. This is not happening for our other similar models, just this one.
Note that I also tried:
Role.findById(_id).lean().exec()
Role.findById(_id).exec()
Role.findOne({_id}).exec()
and have the same issue. when I got to the shell, I run:
rs0:PRIMARY> db.roles.find({_id:ObjectId("5ade466c1384a4deffac6b09")})
{ "_id" : ObjectId("5ade466c1384a4deffac6b09"), "active" : true, "functionalGroup" : "IT", "defaultCategories" : [ "CATEGORY_GENERAL", "CATEGORY_NETWORK_LANDSCAPE", "CATEGORY_CALL_CENTER", "CATEGORY_TELECOMMUNICATIONS", "CATEGORY_VIDEO" ], "key" : "ROLE_XXX", "name" : "ROLE_XXX", "createdBy" : "alexamil", "createdAt" : ISODate("2018-04-23T20:47:40.413Z"), "updatedBy" : "alexamil", "updatedAt" : ISODate("2018-04-23T20:47:40.413Z"), "__v" : 0 }
anyone know what's going on?
I am on Mongoose version: 5.0.16, and Mongo version: 3.4.x
The schema definition for Role is:
'use strict';
import {Schema, Document, model, Model} from 'mongoose';
import {CDTBase, CDTBaseModel} from "../base-model";
/////////////////////////////////////////////////////////////////////
const rolesSchema = new Schema(Object.assign({
_id: {
type: Schema.Types.ObjectId,
required: true,
auto: true
},
key: {
// the canonical identifier (all uppercase, etc)
type: String,
required: true
},
name: {
// the display name
type: String,
required: true
},
active: {
// if we wish to deactivate this role, we would have to
// change all users with this role to a different existing role
type: Boolean,
default: true
},
functionalGroup: {
// functional group key
type: String,
// required: true,
default: 'IT'
},
defaultCategories: {
// default category keys
type: [String],
// required: true,
default: []
},
persona: String,
}, CDTBase));
rolesSchema.index({name: 1}, {unique: true, background: false});
rolesSchema.index({key: 1}, {unique: true, background: false});
export interface CDTModel extends CDTBaseModel, Document{
key: string,
name: string,
active: boolean,
functionalGroup: string,
defaultCategories: Array<string>,
persona: string
}
export const cdtmodel = model<CDTModel>('Role', rolesSchema, 'roles');
base-model.ts just looks like:
export interface CDTBaseModel {
createdAt?: Date,
updatedAt?: Date,
updatedBy?: string,
createdBy?: string
}
export const CDTBase = {
createdBy: {
type: String,
required: false
},
updatedBy: {
type: String,
required: false
},
createdAt: {
type: Date,
required: false
},
updatedAt: {
type: Date,
required: false
}
};
Upvotes: 0
Views: 300
Reputation: 167
try with this please
Role.findById({_id},function (err,role) {
if (err) throw err;
if(!role){console.log("role not found")}
else{
caches.RolesCache[role.key] = role;
caches.RolesCacheById[role._id] = role;
}
});
Upvotes: 0
Reputation: 7344
Change the query from
Role.findById(_id, {lean: true})
to
Role.findById(_id).lean()
According to their docs, the second argument is the projection, and it misses the key
.
Upvotes: 1