-
Notifications
You must be signed in to change notification settings - Fork 60
Getting the current user object #22
Comments
Have you published you user on the server:
and subscribed on the client:
Regarding the profile property, I'm not 100% sure but I'm thinking something like this might work:
then just find the email like so:
|
Hi ashmore11, Thanks for your reply. I have published a user via the Meteor server. I'll post my current code: Meteor Server:
Swift code:
User.swift
DashboardViewController.swift
After logging in the Dashboard VC is loaded and the print result in a crash.. |
Are you calling your logIn function on the Server class anywhere? Also, shouldn't this:
not be this:
|
I'm calling the logIn method on the LoginViewController.swift
I changed it to currentUser now but no difference, it still crashes. |
Are you able to get any information from the crash? If you change the Meteor log level to Info, are there any clues? |
This looks generally correct to me. It would be helpful to know why the compiler says it's crashing? Could you post the crash message? |
|
@ashmore11 Unfortunately the log info does not provide any clues. It just subscribes and crashes and provides me with the crash message as above. |
MH.currentUser refers to an instance of MeteorCollection, which is not key-value coding compliant. That's what it's telling you. Getting the current user the way you have it set up should look something like this:
That should return your user, as an instance of your User class. MeteorCollection is pretty basic. Have a look at the methods it offers. It's probably just fine for user management, but for bigger problems, you might want to consider building on top of it - adding/overriding methods of your own or rolling your own. |
@siegesmund Thanks for your reply. I've changed my code in the DashboardViewController to:
When I run the app it crashes with this fatal error:
|
You have two optionals in that code, either of which could be causing the error, but you haven't specified which one. I'm betting it's the second one. If it's the first one - Meteor.client.userId() - that indicates that your login hasn't completed. But as I read your code, it seems your login should have completed. So... A couple of things to note: Pub/Sub isn't instant. After you login - which is async and takes a little bit of time, then you're subscribing to currentUser, which is async and takes a little bit of time to send the sub request, and to get the pub data - in this case your user. Looking at your login function, you call the callback before the publication has finished, triggering the invocation of your dashboard VC, which expects the publication to be ready/finished. But it's async and probably won't be. That means your user collection will still be empty. That means your currentUser will be nil if you force unwrap it. Putting your callback in the subscription completion closure will ensure that it runs only after the sub is complete. I don't know if this will solve all of your problems here, but It should pop one off of the stack.
|
Hi @siegesmund I logged the Meteor.client.userId() and it was filled so the problem is not there. I've added the completion closure to my logIn method, but it still crashes on the same point when I try to log the profile.. |
If you're logged in, and you can get the client userId, then this is a problem with your implementation of MeteorCollection. As @ashmore11 suggested - the log info is going to be your friend here. Set log level to debug. You'll be able to see every DDP message that is passed between your client and server. That will make is easy to determine exactly what the client is receiving - when it gets your user data, and what user data was passed. |
This is the debug log from app start to crash..
|
Notice how the message says 'collection=users'? Change your user collection to I see you changed it earlier in the thread to 'currentUser'. That's the name of your publication, not your collection. |
@siegesmund Yes, I've noticed that but unfortunately it does not solve the problem. |
I think you can implement the User class something similar to given below:
setValue is the method which will get called when documentWasAdded method get's called and set the attributes in the User class. I think this is the method which will actually map the NSDictionary data to our User class and set the attributes. |
Yep, got it working. @Sef1995 I can post a code example if you're still having trouble. The key is to override setValue. Might be good to add a little example for people in the docs? Anyways this issue can be closed. Edit: Maybe overriding setValue in MeteorDocument is a good idea, catch the exception if can't set the value? |
@SwapnilBGaikwad Thanks for your reply, I got it working now by overriding it just like you suggested! |
It looks like you've figured this out, so I'm closing this issue. |
Hello iain17 , could you post your example code? |
Hi there love the library. However I'm having trouble getting the current user object after being logged in. I'm curious about the fact if I need to manually subscribe after logging in or will it do it automatically?
Also is there any example code of instructions I might've missed that implement the basics.
Also I'd like to add that I'm using Meteor accounts and I'm wondering how nested properties like the "profile" property will look like class wise.
Any help at all would be very welcome! Excellent job so far!
The text was updated successfully, but these errors were encountered: