Sunday, 11 June 2017

500 error: Cast to ObjectId failed for value

I get the error from the server.

"{"message":{"message":"Cast to ObjectId failed for value \"authors\" at path \"_id\" for model \"Comment\"","name":"CastError","stringValue":"\"authors\"","kind":"ObjectId","value":"authors","path":"_id"}}"

I have the model:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var commentSchema = new Schema({
  author: String,
  title: String,
  text: String,
  favorite: Number
});

var Comment = mongoose.model("Comment", commentSchema);

module.exports = Comment;  

From the client which written by Angular 2 I send the request using the method:

getAuthors() {
         return this.http.get(this.appConfig.urlServer + "/comment/authors")
             .map((response: Response) => response.json());
}  

I have next route:

var express = require("express");
var mongoose = require("mongoose");
var Comment = require("../models/comment");
var router = express.Router();

router.get("/comment/authors", getAuthorsOfComments);

module.exports = router;

function getAuthorsOfComments(req, res) {
    Comment.find({}, 'author', function(err, authors) {
    if (err) {
      res.status(500).json({
        message: err
      })
    }

    if (authors) {
      res.status(200).json({
        authors: authors
      })
    }
  })
}

Please, help me. I don't know why I get error.



via Alexey Korkoza

No comments:

Post a Comment