mern-starter
mern-starter copied to clipboard
Cast Error in API call
Hi Joshua:
When I execute the api call to: http://localhost:8080/dashboard/profile I get the following error from the server: CastError: Cast to ObjectId failed for value "undefined" at path "_id".
I'm running the latest versions of everything in both client and server packages. It doesn't seem to be obtaining the _id from MongoDB. Have you seen this before?
BTW: Posting to the DB and registering etc. works fine.
Cheers, Dave
Hi Dave,
I believe your problem is going to be fixed around here: https://github.com/joshuaslate/mern-starter/blob/master/server/config/passport.js#L39
Try console logging payload on line 41. For some reason, I've had a few people who are finding their _id elsewhere on the payload object. You'll have to manually track down where the data is coming through on this payload object sand update your code that is currently on line 41 (User.findById(payload._id, function(err, user)) accordingly.
Please let me know if this works for you.
Thank you,
Josh
Hi Joshua:
The payload looks OK (to me at least): ... OPTIONS /api/user/undefined 200 8.564 ms - 8 { _id: '58066b85c60c79687410b0b7', firstName: 'Dave', lastName: 'Cook', email: '[email protected]', role: 'Member', iat: 1477928908, exp: 1477938988 } GET /api/protected 304 123.253 ms - - { _id: '58066b85c60c79687410b0b7', firstName: 'Dave', lastName: 'Cook', email: '[email protected]', role: 'Member', iat: 1477928908, exp: 1477938988 } GET /api/user/undefined 400 20.596 ms - 47 CastError: Cast to ObjectId failed for value "undefined" at path "_id" ..... BTW: I'm calling it just after I login on a side-navigation component: ... class Navigation extends Component {
componentWillMount() { // Fetch user data prior to component mounting const userId = cookie.load('uid'); this.props.fetchUser(userId); } ... Dave
Okay, so it seems your client-side is making a request with an undefined user id: /api/user/undefined
Try console logging userId const right after you load the uid cookie and before you fetchUser. Is that coming up undefined?
Interesting.
Yep, the console is showing nothing. I don't think const userId = cookie.load('uid');
is even getting fired. I tried a fresh install of mern-starter, thinking it might be my stuff, but I got the following when logged in and calling: http://localhost:8080/profile -
GET http://localhost:3000/api/user/undefined 400 (Bad Request) Uncaught (in promise) TypeError: Cannot read property 'error' of undefined(…)
Cheers, Dave
Solved - In my Navigation component I did this by just grabbing the cookie contents:
...
class Navigation extends Component {
constructor(props) {
super(props);
this.state = { profile: cookie.load('user') };
}
...
Then I displayed what I needed in the render / return statements with:
...
{this.state.profile.email}
(or whatever cookie item I needed, _id, role etc...)
...
Note: I tried using this.props.fetchUser();
but I kept getting:
TypeError: this.props.fetchUser() is not a function
. I must have been calling it wrong or something but I couldn't figure it out for now. Anyway, all up and running.
BTW: I've integrated the mern-starter with the Inspinia theme so I can start making some nice elements for rapid prototyping. I'll let you know when it's up - hopefully this week. I might give the social login thingy a shot as well.
Cheers, Dave
Dave,
That's great you were able to solve this. Sorry, I've been pretty busy so it's tough to be super responsive.
I look forward to seeing your work in combining the mern-starter with Inspinia! Good luck with the social integration. I'd love if you submitted a PR with social integration if you get that working.
Thanks,
Josh
Hi Joshua:
No problem, I understand the busy thing all too well (I was once a CTO of a brokerage house). I was going to ask you about PR's so cool I'll send one over once I get it going.
Cheers, Dave
On Tue, Nov 1, 2016 at 12:56 PM, Joshua Anderson Slate < [email protected]> wrote:
Dave,
That's great you were able to solve this. Sorry, I've been pretty busy so it's tough to be super responsive.
I look forward to seeing your work in combining the mern-starter with Inspinia! Good luck with the social integration. I'd love if you submitted a PR with social integration if you get that working.
Thanks,
Josh
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/joshuaslate/mern-starter/issues/3#issuecomment-257622904, or mute the thread https://github.com/notifications/unsubscribe-auth/ASt2yFWdToqqCOKSgDU5mK7eZmXBKsECks5q528-gaJpZM4KlJaI .
Hi:
Just an fyi, I put up what I have so far: https://github.com/gridworkz/griderp-starter
Cheers, Dave
On Tue, Nov 1, 2016 at 6:51 PM, Dave Cook [email protected] wrote:
Hi Joshua:
No problem, I understand the busy thing all to well (I was once a CTO of a brokerage house). I was going to ask you about PR's so cool I'll send one over once I get it going.
Cheers, Dave
On Tue, Nov 1, 2016 at 12:56 PM, Joshua Anderson Slate < [email protected]> wrote:
Dave,
That's great you were able to solve this. Sorry, I've been pretty busy so it's tough to be super responsive.
I look forward to seeing your work in combining the mern-starter with Inspinia! Good luck with the social integration. I'd love if you submitted a PR with social integration if you get that working.
Thanks,
Josh
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/joshuaslate/mern-starter/issues/3#issuecomment-257622904, or mute the thread https://github.com/notifications/unsubscribe-auth/ASt2yFWdToqqCOKSgDU5mK7eZmXBKsECks5q528-gaJpZM4KlJaI .
Hi,
Awesome! I gave it a star and will watch it. I'm excited to see what you do :)
Good luck,
Josh