So.... I'm trying to get a list of ID's from the database, and I'm trying to do it like this:
router.get('/list', (req,res) => {
Model.findAll({}, {_id}).then(...)
});
and I'm getting this error:
Cast to ObjectId failed for value "list" at path "_id" for model "insiders"
CastError: Cast to ObjectId failed for value "list" at path "_id" for model "insiders"
at new CastError (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\error\cast.js:27:11)
at ObjectId.cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schema\objectid.js:158:13)
at ObjectId.SchemaType.applySetters (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:724:12)
at ObjectId.SchemaType._castForQuery (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1113:15)
at ObjectId.SchemaType.castForQuery (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1103:15)
at ObjectId.SchemaType.castForQueryWrapper (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1082:15)
at cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\cast.js:300:32)
at model.Query.Query.cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\query.js:3309:12)
at model.Query.Query._castConditions (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\query.js:1293:10)
at model.Query.Query._findOne (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\query.js:1518:8)
at process.nextTick (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\kareem\index.js:333:33)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
I'm not sure what this error means, but I'm sure I'm not filtering out the other properties properly.... I think....
Model.findAll()
?
findAll()
instead of just find()
to get "All the things"
Model.find({}, { _id: 1 })
I think hehe
Populate()
but I was still getting the same error.... Think it's time to give out the full Express route though.router.get('/list', (req, res) => {
Insider.find({}, '_id').then(insiders => {
if (!insiders) {
res.status(400).json({ error: 'unable to find list of insiders' });
}
res.json(insiders);
}).catch(err => res.status(400).json(err));
});
'_id'
should be { _id: 1 }
Cast to ObjectId failed for value "list" at path "_id" for model "insiders"
CastError: Cast to ObjectId failed for value "list" at path "_id" for model "insiders"
at new CastError (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\error\cast.js:27:11)
at ObjectId.cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schema\objectid.js:158:13)
at ObjectId.SchemaType.applySetters (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:724:12)
at ObjectId.SchemaType._castForQuery (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1113:15)
at ObjectId.SchemaType.castForQuery (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1103:15)
at ObjectId.SchemaType.castForQueryWrapper (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1082:15)
at cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\cast.js:300:32)
at model.Query.Query.cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\query.js:3309:12)
at model.Query.Query._castConditions (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\query.js:1293:10)
at model.Query.Query._findOne (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\query.js:1518:8)
at process.nextTick (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\kareem\index.js:333:33)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
failed for value "list"
is really troubling.
{ _id: 1}
?
let's see what using the native driver directly does.
replace
Insider.find({}, { _id: 1}).then(etc)
with
Insider.collection.find({},{ _id: 1}).then(etc)
/list
to /jerry
and the error changed from list....
to jerry....
Very odd....
db.insiders.find({ _id: 'list' })
?
res.status(400).json({ error: 'unable to find list of insiders' });
Hi, and help me) for example I have document
{
"index": 2,
"retryAfter": [100, 1000, 10000, 1000000]
}
how to build query to retrieve such element from inner array as value of index field?
Insider.find({}, {_id})
it needs to be Insider.find({}, {_id: 1 })
Model.find()
but Model.find()
just passes the arg off to query's select method. I just tested this out, '+_id'
, on an express route and it works just like the above object.