javascript - Cast to number failed for value "XXX" at path "_id" for model "users" Mongoose Mongodb -
i having problems populating
across 2 different models. user
, player
. each has objectid
stored each other. error occurs when try find following populate
user
model.
user.find().populate('player_id')
data of users player_id
> db.getcollection('users').find({}); { "_id" : objectid("58ea512576f3381d0cdf39a7"), "key" : "c7c7d3611f3d856f7a75eae5303e1cdf", "player_id" : objectid("59b3bff9605cbf7610c15837") }
data of players
> db.getcollection('players').find(); { "_id" : objectid("59b3bff9605cbf7610c15837"), "user_id" : objectid("58ea512576f3381d0cdf39a7") }
user model
var userschema = new schema({ name: string, player_id: {type: schema.objectid, ref: "player", default: null} })
player model
var player = new schema({ fname : string, sname : string, user_id: {type: schema.objectid, ref: "user", default: null}, });
full error message
{ casterror: cast number failed value "59b3bff9605cbf7610c15837" @ path "_id" @ casterror (/var/www/norsemenfc.co.uk/node_modules/mongoose/lib/error/cast.js:27:11) @ schemanumber.cast (/var/www/norsemenfc.co.uk/node_modules/mongoose/lib/schema/number.js:231:9) @ schemanumber.schematype._castforquery (/var/www/norsemenfc.co.uk/node_modules/mongoose/lib/schematype.js:1064:15) @ schemanumber.castforquery (/var/www/norsemenfc.co.uk/node_modules/mongoose/lib/schema/number.js:282:14) @ /var/www/norsemenfc.co.uk/node_modules/mongoose/lib/schematype.js:991:18 @ array.map (<anonymous>) @ schemanumber.handlearray (/var/www/norsemenfc.co.uk/node_modules/mongoose/lib/schematype.js:990:14) @ schemanumber.castforquery (/var/www/norsemenfc.co.uk/node_modules/mongoose/lib/schema/number.js:280:20) @ schemanumber.schematype.castforquerywrapper (/var/www/norsemenfc.co.uk/node_modules/mongoose/lib/schematype.js:1016:17) @ cast (/var/www/norsemenfc.co.uk/node_modules/mongoose/lib/cast.js:250:39) @ model.query.query.cast (/var/www/norsemenfc.co.uk/node_modules/mongoose/lib/query.js:3109:12) @ model.query.query._castconditions (/var/www/norsemenfc.co.uk/node_modules/mongoose/lib/query.js:1145:10) @ model.query.query._find (/var/www/norsemenfc.co.uk/node_modules/mongoose/lib/query.js:1160:8) @ /var/www/norsemenfc.co.uk/node_modules/kareem/index.js:250:8 @ /var/www/norsemenfc.co.uk/node_modules/kareem/index.js:23:7 @ _combinedtickcallback (internal/process/next_tick.js:131:7) @ process._tickcallback (internal/process/next_tick.js:180:9) message: 'cast number failed value "59b3bff9605cbf7610c15837" @ path "_id" model "users"', name: 'casterror', stringvalue: '"59b3bff9605cbf7610c15837"', kind: 'number', value: 59b3bff9605cbf7610c15837, path: '_id', reason: undefined,
[edit]
query call users
var mongoose = require('mongoose') user = mongoose.model('user'), player = mongoose.model('player'); module.exports = { getall: function(passdata){ return user.find().populate('player_id') //point failure happens .then(function(data){ passdata.userdetails = data; return passdata; }); },
how objectid's saved
so use mongoose.types.objectid
make sure, if stored string don't believe should happen.
updateuserid: function (req, res) { var player_id = req.body.user,//string user_email = req.body.saveplayer; //string res.setheader('content-type', 'application/json'); return user.find({ email: user_email }).exec({}) .then(function(data){ var user_id = data[0]._id; //objectid user.update({email: user_email}, {player_id: mongoose.types.objectid((player_id))}, function (err) { if(err) res.send(json.stringify({status: 0})); player.update({_id: player_id}, {user_id: user_id}, function (err) { if(err) { res.send(json.stringify({status: 0})); }else{ res.send(json.stringify({ status: 1 })); } }) }) }); }
edit 2
route
home: function (req, res) { var passdata = {}; api.getall(passdata) .then(api.getplayerswithoutauser) .then(function (passdata) { res.render('admin/users', { user: req.user, title: 'admin users', selectedadminmenu: 'users', passdata: passdata }); }) .catch(function (err) { console.error(err); }); },
i using mongoose ^4.11.11
Comments
Post a Comment