How to refresh user data in React Moralis?

I have a cloud function that adds a value to the user table. But the value is not available on React Moralis unless I log out and in again. I know that it has to do with the local cache of the user data. What I need to know is how can I refresh that data?

Thanks!

If you want to get real time alert when something changes in DB you need to use live-queries https://docs.moralis.io/moralis-sdk/live-queries

Also - please note that react-moralis is not production ready and there may be some bugs, at the moment only vanilla JS SDK is production ready

Hi Ivan. Thanks.
No, I donā€™t need an alert, because I know I just updated the user field. But what I need to find out is how to get the local cached User Object to update with the data in the DB.

Moralis.User.currentAsync().then(function(user) {
        console.log(user);//<-- this user object is also stale
    });

Ah use ā€œfetchā€ when you want to refresh local object

https://parseplatform.org/Parse-SDK-JS/api/v1.11.1/Parse.Object.html#fetch

You can use ā€œdirtyā€ and ā€œdirtyKeysā€ to keep track of changed fields - see same link

If you want to know from server when something changed like an alert - use live query on User table

Interesting.
A quick test of Moralis.User.fetch() didnā€™t work. It said that it wasnā€™t a function.
Iā€™ll give it another spin in a while, to see what Iā€™m doing wrong.

Thank you!

@krb did you ever get this to work? Iā€™m running into the exact same issue. I would expect useMoralis to rerun and get the latest user once I finish calling my cloud function using the useMoralisCloudFunction hook. Using fetch doesnā€™t cause the cache to update.

Hey @acorredor

Iā€™ve checked Moralis.User.fetch() didnā€™t waork for me too.Currently you can use live queries. But weā€™ll fix the bug soon, so you can do something else, for example.

Iā€™ll let you know when it will be fixed :raised_hands:

1 Like

Itā€™s all good, I actually just used the onSuccess from the cloud functionā€™s fetch method to call setEmail, which just updates the email locally. That just works for my current use case. But yeah, having a nice way to manage the user cache would be nice (with automatic updates, similar to apollo client).

Hey @acorredor

I was a little wrong. You need to use Moralis.User.current().fetch() - it works

In version 0.2.2 of react-moralis this is now possible: https://github.com/MoralisWeb3/react-moralis#refetch-user-data-refetchuserdata

1 Like