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 Error in API call #3

Open
gridworkz opened this issue Oct 31, 2016 · 9 comments
Open

Cast Error in API call #3

gridworkz opened this issue Oct 31, 2016 · 9 comments

Comments

@gridworkz
Copy link

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

@joshuaslate
Copy link
Owner

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

@gridworkz
Copy link
Author

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

@joshuaslate
Copy link
Owner

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.

@gridworkz
Copy link
Author

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

@gridworkz
Copy link
Author

gridworkz commented Nov 1, 2016

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

@joshuaslate
Copy link
Owner

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

@gridworkz
Copy link
Author

gridworkz commented Nov 1, 2016

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
#3 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ASt2yFWdToqqCOKSgDU5mK7eZmXBKsECks5q528-gaJpZM4KlJaI
.

@gridworkz
Copy link
Author

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
#3 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ASt2yFWdToqqCOKSgDU5mK7eZmXBKsECks5q528-gaJpZM4KlJaI
.

@joshuaslate
Copy link
Owner

Hi,

Awesome! I gave it a star and will watch it. I'm excited to see what you do :)

Good luck,

Josh

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

2 participants