Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast to ObjectID Failed #150

Open
bnurick opened this issue Jan 5, 2017 · 2 comments
Open

Cast to ObjectID Failed #150

bnurick opened this issue Jan 5, 2017 · 2 comments

Comments

@bnurick
Copy link

bnurick commented Jan 5, 2017

I am receiving this on all attempts to GET or DELETE by ID. I was trying a very simple implementation to learn how to build simple REST APIs.

I have, what I thought, was a very simple MEAN stack example.

MODEL DEFINITION:
// Dependencies
var restful = require('node-restful');
var mongoose = restful.mongoose;

// Schema
var productSchema = new mongoose.Schema({
name: String,
sku: String,
price: Number,
updated_at: { type: Date, default: Date.now }
});

// Return model
module.exports = restful.model('Products', productSchema);


I was able to add via POST, and this is returned using the GET to /api/products:
[{"_id":"586d9d3b3d618b1d481a395f","name":"Test Product","sku":"lkiuhygs","price":12.99,"__v":0,"updated_at":"2017-01-05T01:11:23.478Z"}]

So, the POST works, GET all works.

Any PUT or GET specific by the ID errors, example GET request below, and the error.
GET request sent in POSTMAN:
http://localhost:3000/api/products/586d9d3b3d618b1d481a395f

Error returned in POSTMAN:
{"message":"Cast to ObjectId failed for value "586d95629202a834e871eff9" at path "_id" for model "Products"","name":"CastError","stringValue":""586d95629202a834e871eff9"","kind":"ObjectId","value":"586d95629202a834e871eff9","path":"_id"}

I admit I am just starting out on this, so the issue may be with me!

@rbalmf
Copy link

rbalmf commented Jan 10, 2017

Just a guess, but looking at the error above it seems as though there is a problem record in Mongo where _id is quoted (""586...""), maybe that is causing a problem? Try clearing down your data and making sure that that the new record you post isn't quoted in the DB.

@OmgImAlexis
Copy link

@bnurick you can you provide what you sent via POST? I'd have to agree with @rbalmf as the _id looks like it's a string value starting and ending with ".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants