Sunday, 19 March 2017

Mongoose schema and find cannot populate

I have the following schema:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.Promise = global.Promise;

var userSkillSchema = Schema ({
    email:{type:String, required:true, ref:'User'},
    skill:{type:String, required:true},
    sub1:{type:String, required:true},
    sub2:{type:String, required:true},
    experience:{type:Number, required:true}
});

var UserSkill = mongoose.model('UserSkill',userSkillSchema);
module.exports = UserSkill;

When I am doing a simple UserSkill.find as follow:

router.get('/getskill/:email', function(req, res){
    UserSkill.find({email:req.params.email})
    .populate('email')
    .exec(function(err, userskill){
        if (err) return res.send(err);
        else{
            res.json({userskill});
        }
    });
});

I am getting the following error:

{
  "message": "Cast to ObjectId failed for value \"john@gmail.com\" at path \"_id\" for model \"UserSkill\"",
  "name": "CastError",
  "stringValue": "\"JohnDoe@gmail.com\"",
  "kind": "ObjectId",
  "value": "JohnDoe@gmail.com",
  "path":

In the documentation it is written that I can use String, Number, ObjectId, Buffer as Ref. Which I did. But still I am getting errors.



via user3450754

No comments:

Post a Comment