2

Disclaimer: I'm new to Meteor.

I'm working on some meteor client code that runs as a template helper and loads the username of a user with the provided id:

 Template.message.helpers
    editedBy: ->
        return "" unless wasEdited(@)
        if @edit.by
            user = Meteor.users.findOne(@edit.by)
            if user?
                user.username
            else
                "?"
        else
            "?"

The issue with this code is that when it is invoked by the message template with the non-reactive render helper:

{{#each messagesHistory}}
  {{#nrr nrrargs 'message' .}}{{/nrr}}
{{/each}}

and the message template is of the form:

<template name="message">
...
    {{editedBy}}
...
</template>

The function returns ? since the Meteor lookup returns undefined. After debugging and adding some logging, I can verify that the @edit.by id is the correct user id, and that calling Meteor.users.findOne(thatId) loads the correct user (when I do it via the chrome javascript console).

What's going on here? I'm guessing it has something to do with when the data is available, but how do I fix it?

Thanks in advance!

Debugging Info

During App Load

During App Load

After App Load

After App Load

Blaskovicz
  • 6,122
  • 7
  • 41
  • 50
  • Code looks good to me. It's hard to tell exactly what's happening without more context. My guess is that your helper is run too soon, and/or is not reactively re-run when its context changes. If you console.log(@edit.by) inside your helper, does it show the correct id ? – looshi Nov 08 '15 at 19:35
  • Yes, it does. [Here](https://github.com/RocketChat/Rocket.Chat/pull/1357/files) is the full code if you're interested. – Blaskovicz Nov 08 '15 at 19:43
  • I did more looking in the code, and it seems like it's using the [non-reactive render' package](https://github.com/Konecty/meteor-nrr) for message templates – Blaskovicz Nov 08 '15 at 20:21
  • Did you write the above code? I'm confused by your comment that "It seems like it's using the non-reactive render' package". Do you want to use this package? Did you add it to your project? – JeremyK Nov 08 '15 at 20:27
  • I didn't add the package to the project, I'm just trying to enhance the message to support showing "edited by." I'm trying to work around the existing constraints, assuming that (like the github for the nrr helper says) the rocketchat maintainers used the helper for performance reasons. I've updated my main post to include more context about the `nrr` finding. – Blaskovicz Nov 08 '15 at 20:35
  • OK Meteor.users is not `auto-published`, so first check what data is available on the client. Add `console.table Meteor.users.fetch()` `console.log@edit.by` `console.log Meteor.users.findOne(@edit.by)` below `if @edit.by` – JeremyK Nov 08 '15 at 20:38
  • @JeremyK - I added the debugging information. – Blaskovicz Nov 08 '15 at 20:54
  • Suggest to add an issue in Rocket.Chat github and describe that you need this user data subscription to be ready at the time the message template is rendered. You could either alter the flow of the pub/sub, or add a new server Meteor method, or even de-normalize the user data and store it directly onto the message. The solution probably needs some consensus. – looshi Nov 08 '15 at 21:03
  • 1
    So there you can see that when your helper is called the data is not available. If this was reactive, the data would appear when it becomes available. If this is not an option you have to ensure the data is available. You will need to look at your subscriptions, and see if you can check they are ready. Or as @looshi suggests, include the username on the document (probably the best option). I'd avoid calling server methods in the helper though if you can avoid it. If you must, [see here](http://stackoverflow.com/questions/22147813/how-to-use-meteor-methods-inside-of-a-template-helper) – JeremyK Nov 08 '15 at 21:03
  • try to remove the test `if @edit.by` this should improve the reactivity and will not make any difference in your case as querying for `undefined` will return `undefined` – Micha Roon Nov 09 '15 at 08:32

0 Answers0