WP-API/client-js

Get the current user in JS Client

joneslloyd opened this issue · 7 comments

Hi there,

This is maybe(?) a special case, and I definitely can't find any documentation for it with regards to achieving this with the JS Client.

I'm wondering how I get the current user's Model?

Do I simply put:

var current_user = new wp.api.models.Users( { 'me' } ); ?

(I'm guessing that's not the case)

I just can't find an attribute to pass that would allow for the /me/ part.

Any information would be great.

I'm aware that I can print the current user's ID to the page as a Javascript variable, and then pass that in as the ID parameter, but I looked at the "to be added" file (https://github.com/WP-API/client-js/blob/2ff7164eec4bf5229d17eb3c1ac7a476db875c46/routes-to-add.md) and it says that /users/me/ has been added.

Thanks

@joneslloyd Thanks for bringing this up. The 'me' endpoint is a special case and support was broken in the client. I pushed an update to master in #92 that corrects the issue.

You can now get the current user model with:

var me = new wp.api.models.Me();
ver meFetchPromise = me.fetch(); 
meFetchPromise.done( function() {
// ... do something with me
} );

Can you please test and let me know if this resolves your issue?

screenshot

Hi @adamsilverstein - thanks!

When I run this, I get Uncaught TypeError: wp.api.models.Me is not a function unfortunately.

I'm just looking over my code again to make sure there's not a mistake.

Doh - I haven't got the latest version - that'd be why. I'm using the beta WP-API wordpress plugin. Can I get the current js client and merge that with the plugin somehow?

@joneslloyd - Install this repo as a plugin (using master) and activate it. That should cause the client-js plugin JS library to be loaded vs the WP-API plugin. Let me know if that works, haven't tested in a while :)

Thanks - I'll do that!

Works perfectly! Thanks very much :)

Glad to hear, thanks for bringing up the issue and testing.