0

I've been following the online tutorial at thinkster "building your first mean stack app" at

https://thinkster.io/mean-stack-tutorial#beginning-node

im trying to test my route to see if its correct by using

    curl -X PUT http://localhost:8080/posts/<POST ID>/upvote

and mongod throws a 505 error saying

    <h1>Cast to ObjectId failed for value &#34;57bcdb57bcdb255600e6d114e7afb9&#34; at path &#34;_id&#34;</h1>

CastError: Cast to ObjectId failed for value "57bcdb57bcdb255600e6d114e7afb9" at path "_id"
    at MongooseError.CastError (/home/ubuntu/workspace/flapper-news/node_modules/mongoose/lib/error/cast.js:19:11)
    at ObjectId.cast (/home/ubuntu/workspace/flapper-news/node_modules/mongoose/lib/schema/objectid.js:147:13)
    at ObjectId.castForQuery (/home/ubuntu/workspace/flapper-news/node_modules/mongoose/lib/schema/objectid.js:187:15)
    at cast (/home/ubuntu/workspace/flapper-news/node_modules/mongoose/lib/cast.js:208:32)
    at Query.cast (/home/ubuntu/workspace/flapper-news/node_modules/mongoose/lib/query.js:2653:10)
    at Query.findOne (/home/ubuntu/workspace/flapper-news/node_modules/mongoose/lib/query.js:1284:10)
    at /home/ubuntu/workspace/flapper-news/node_modules/mongoose/lib/query.js:2230:21
    at new Promise.ES6 (/home/ubuntu/workspace/flapper-news/node_modules/mongoose/lib/promise.js:45:3)
    at Query.exec (/home/ubuntu/workspace/flapper-news/node_modules/mongoose/lib/query.js:2223:10)
    at /home/ubuntu/workspace/flapper-news/routes/index.js:37:9
    at paramCallback (/home/ubuntu/workspace/flapper-news/node_modules/express/lib/router/index.js:404:7)
    at param (/home/ubuntu/workspace/flapper-news/node_modules/express/lib/router/index.js:384:5)
    at Function.process_params (/home/ubuntu/workspace/flapper-news/node_modules/express/lib/router/index.js:410:3)
    at next (/home/ubuntu/workspace/flapper-news/node_modules/express/lib/router/index.js:271:10)
    at Function.handle (/home/ubuntu/workspace/flapper-news/node_modules/express/lib/router/index.js:176:3)
    at router (/home/ubuntu/workspace/flapper-news/node_modules/express/lib/router/index.js:46:12)

I checked to see if id and object existed by entering

    curl http://localhost:8080/<POST ID>/posts 

and it verified that an object was indeed there

here is my cloud9 workspace if you want to take a closer look cloud9Workspace

i have no idea what went wrong, could it be possible that it has to do with my mongodb version? (its a later one than in the tutorial)

thank you in advance for your time , hope you can help!

alex diaz
  • 1
  • 2
  • Possible duplicate of [What's Mongoose error Cast to ObjectId failed for value XXX at path "\_id"?](http://stackoverflow.com/questions/14940660/whats-mongoose-error-cast-to-objectid-failed-for-value-xxx-at-path-id) – Ajitej Kaushik Aug 24 '16 at 05:09
  • explain why you think its a duplicate, I already went over that previous question and theres no solution for me there – alex diaz Aug 24 '16 at 15:37

1 Answers1

1

It looks like the string you are passing as an ObjectID is malformed (it's too long). Mongo ObjectIDs are 24 characters (12-byte) strings. Your particular ID has 30 characters

Dave V
  • 1,966
  • 9
  • 18
  • My objectID is 24 characters .... alexaxel98:~/workspace/flapper-news $ curl http://localhost:8080/posts [{"_id":"57b8a432d4b2485263e88966","title":"test","link":"http://test.com","__v":0,"comments":[]} – alex diaz Aug 25 '16 at 00:48
  • But if you notice, that's not what's being called out in the error message. Mongoose is getting "57bcdb57bcdb255600e6d114e7afb9" – Dave V Aug 25 '16 at 02:19