Global user object return null

From the documentation it seems as if the user object is made available globaly and thus would not require declarion to use.

https://docs.journeyapps.com/reference/container/user-related-data

However, I am having some trouble getting consistent results, especially when testing locally, in that the user function sometimes returns null, which should not be possible?

The below code snippet should simply log to the console.

function showUserInfo() {
    console.log('Booyah user is:',user);
}

Problem is that I the log shows "Booyah user is:, null"

So my questions from this snippet are:

  1. Can I access the user object globally as the documentation suggests?
  2. What would the circumstances be for the user object to ever be null?

Ok looks like typically as soon as you visit the doctor you no longer have a rash...

I found my problem was caused by my sync rules - so I answered my own questions as follows for the case someone else has a blonde gotcha like this:

Can I access the user object globally as the documentation suggests? Yup

What would the circumstances be for the user object to ever be null? If you omit the user object from your sync rules.

I had very specific sync rules and just added the user model back to the global sync bucket

   <global-bucket>
        <model name="user" />
    </global-bucket>

Unless you need all the users objects to be synced, consider just syncing your own user, as follows.

The user object is available globally as a global JS variable, and so your example code should work.

You can even access it from the view XML as follows.

{$:user} 

However, as you figured out, that assumes the object is being synced.

Your suggested fix will work as it will sync all user objects.

If you would prefer to only sync your own user, you can use the following sync-rules syntax instead and just add an "empty" object-specific bucket with a bucket root of self.

<bucket via="self">
</bucket>