Xz3s
Xz3s

Reputation: 3

Mongoose cast error when populate

I know this question is already asked a lot but I really can't figure it out.

I have two mongoose schemas set up, a user schema and a questions schema:

user schema:

const mongoose = require('mongoose');
const passportLocalMongoose = require('passport-local-mongoose');

var UserSchema = mongoose.Schema({
  uid: String,
  name: String,
  username: String,
  insertion: String,
  surname: String,
  email: String,
  password: String,
  birthday: String,
  gender: String,
  avatar: String,
  school: String
});

UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", UserSchema);

and the questions schema:

const mongoose = require('mongoose');

var vragenSchema = new mongoose.Schema({
question: String,
lesson: String,
answered: Boolean,
author: {
  id: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User"
  },
  username: String
},
date: {
  type: Date,
  default: Date.now
},
description: String,
answersCount: Number,
user_votes: Number,
answers: [{
  type: mongoose.Schema.Types.ObjectId,
  ref: "Answers"
}]
});

module.exports = mongoose.model("Vragen", vragenSchema);

when I try to populate the file with this code:

app.get("/vragen/:id", isLoggedIn, function(req, res) {
  Vragen.findById(req.params.id).populate("author").exec(function(err,gevondenVraag) {
    if (err) {
      console.log(err);
    } else {
      res.render("vragen/show.ejs", {vragen: gevondenVraag})
    }
   });
});

it gives me a cast error:

{ CastError: Cast to ObjectId failed for value "{ id: '5840a19db36cd81788505018', username: 'TygoD' }" at path "_id"
  at MongooseError.CastError (F:\PWS\node_modules\mongoose\lib\error\cast.js:26:11)
  at ObjectId.cast (F:\PWS\node_modules\mongoose\lib\schema\objectid.js:147:13)
  at ObjectId.castForQuery (F:\PWS\node_modules\mongoose\lib\schema\objectid.js:187:15)
  at F:\PWS\node_modules\mongoose\lib\schematype.js:868:18
  at Array.map (native)

  at ObjectId.handleArray (F:\PWS\node_modules\mongoose\lib\schematype.js:867:14)
  at ObjectId.castForQuery (F:\PWS\node_modules\mongoose\lib\schema\objectid.js:185:20)
  at cast (F:\PWS\node_modules\mongoose\lib\cast.js:213:39)
  at Query.cast (F:\PWS\node_modules\mongoose\lib\query.js:2741:10)
  at Query.find (F:\PWS\node_modules\mongoose\lib\query.js:1144:10)
  at F:\PWS\node_modules\mongoose\lib\query.js:2300:21
  at new Promise.ES6 (F:\PWS\node_modules\mongoose\lib\promise.js:45:3)
  at Query.exec (F:\PWS\node_modules\mongoose\lib\query.js:2294:17)
  at populate (F:\PWS\node_modules\mongoose\lib\model.js:2709:11)
  at _populate (F:\PWS\node_modules\mongoose\lib\model.js:2615:5)
  at Function.Model.populate (F:\PWS\node_modules\mongoose\lib\model.js:2575:5)
message: 'Cast to ObjectId failed for value "{ id: \'5840a19db36cd81788505018\', username: \'TygoD\' }" at path "_id"',
name: 'CastError',
kind: 'ObjectId',
value: { id: '5840a19db36cd81788505018', username: 'TygoD' },
path: '_id',
reason: undefined }

I tried several solutions but those didn't help. Is this a bug or did I do something wrong?

Also I am new to node.js and mongodb, so if there are any improvements I can make please tell me.

Upvotes: 0

Views: 1686

Answers (2)

Mohsen ZareZardeyni
Mohsen ZareZardeyni

Reputation: 956

Your id class is not ObjectId! mongo stores a normal DBRef like this:

id: DBRef("User", ObjectId("5840a19db36cd81788505018"))

So either you need to change your id type from ObjectId or you need to fix your insertion and data on the server!

Upvotes: 0

Aruna
Aruna

Reputation: 12022

It should be author.id in your populate instead of author which you can try as below.

app.get("/vragen/:id", isLoggedIn, function(req, res) {
  Vragen.findById(req.params.id).populate("author.id").exec(function(err,gevondenVraag) {
    if (err) {
      console.log(err);
    } else {
      res.render("vragen/show.ejs", {vragen: gevondenVraag})
    }
   });
});

Upvotes: 1

Related Questions