Meteor session and reactivevar not working reactively

please help me I’m going crazy.

I have two pages, fila.js and retirada.js.

fila.js contains a function called ‘atualizarChamadas’ that I export to retirada.js.

When I call the function from fila.js it works fine, but when I call it in retirada.js the session object or reactive-var don’t work reactively in the helper.

My function that are in fila.js:

export default function atualizarChamadas() {

    let chamadas = Chamadas.find({medico: 1},{sort: {data_inicio: -1}}).fetch();
    //Session.set("chamadas", chamadas);
    ChamadasReactive.set(chamadas);
}

And My templates helpers

 Template.fila.helpers({
    chamadasAtendimento: function() {
        //return Session.get("chamadas");
        return ChamadasReactive.get().filter((el)=>{
            return el.status == 'A';
        }).slice(0,11);
      }
});
Template.fila.helpers({
    chamadasEspera: function() {
        //return Session.get("chamadas");

        return ChamadasReactive.get().filter((el)=>{
            return el.status == 'E';
        }).slice(0, 11);
      }
});

Template.fila.helpers({
    chamadasFinalizada: function() { 
        return ChamadasReactive.get().filter((el)=>{
            return el.status == 'F';
        }).slice(0, 11);
      }
});

In fila.js I call the function like that and it works fine:

Template.fila.events({
        "submit form": function (e, template){
            e.preventDefault();
            atualizarChamadas();
        }

});

I have this blaze template that as I said works fine when the functions is called from fila.js:

<tbody>  
        {{#each chamada in chamadasEspera}}
            <tr class="success">
                <td>{{chamada.senha}}</td>
                <td>{{chamada.data_inicio}}</td>
                <td></td>
                <td>
                    <select id="{{chamada._id}}" class="form-control">
                        <option value="E">Esperando</option>
                        <option value="A">Atendimento</option>
                        <option value="F">Finalizado</option>
                    </select>
                </td>
                <td><button id="{{chamada._id}}" value="{{chamada.senha}}" name="{{chamada.status}}" class="btn btn-block">Chamar</button></td>

            </tr>
         {{/each}}
    </tbody>


    <br/>

    <tbody>
    {{#each chamada in chamadasAtendimento}}
            <tr class="danger">
                <td>{{chamada.senha}}</td>
                <td>{{chamada.data_inicio}}</td>
                <td></td>
                <td>
                    <select id="{{chamada._id}}"  class="form-control">
                        <option value="A">Atendimento</option>
                        <option value="E">Esperando</option>
                        <option value="F">Finalizado</option>
                    </select>
                </td>
                <td><button id="{{chamada._id}}" value="{{chamada.senha}}"  name="{{chamada.status}}" class="btn btn-block">Chamar</button></td>
             </tr>
     {{/each}}
    </tbody>

    <br/>
    <tbody>
    {{#each chamada in chamadasFinalizada}}
        <tr class="info">
            <td>{{chamada.senha}}</td>
            <td>{{chamada.data_inicio}}</td>
            <td>{{chamada.data_fim}}</td>
            <td>
                <select id="{{chamada._id}}"  class="form-control">
                    <option value="F">Finalizada</option>
                    <option value="E">Esperando</option>
                    <option value="A">Atendimento</option>
                </select>
            </td>
            <td></td>
        </tr>
    {{/each}}
     </tbody>
  </table>
  {{/if}}

In my page retirada.js I’ve this code that call my function ‘atualizarChamadas’, but it does not working reactively:

 Template.retirada.events({

    "click button": function(e,template){

        let newPass = e.target.id,
            id = Retirada.find({}).fetch()[0]._id;

        console.log('01:' + Chamadas.find({medico: 1},{sort: {data_inicio: -1}}).fetch().length);

        Retirada.update(id, {count: newPass});
        Chamadas.insert(
            {senha: newPass, status : 'E', data_inicio: new Date(), medico: 1});

        console.log('02:' + Chamadas.find({medico: 1},{sort: {data_inicio: -1}}).fetch().length);

        setTimeout (teste, 1000);

        function teste(){
            atualizarChamadas();
        }
    }
     });

When the console.log is called, I can see the length of object result of chamadas increased, but my template does not refresh the new data. I was used methods to change the document, but I change it for doing on the view, but still does not work, please, I need some help!

Can I see the code where you initialize ChamadasReactive?

In my experience, you don’t want to do the query, and THEN set it to reactive var. Reactive vars are just for when you want a reactive piece of data that’s NOT in the mongo db.

Try changing your helper to:

Template.fila.helpers({
    chamadasEspera: function() {
       //this Chamadas.find() .fetch() is already reactive. Control which chamadas you
       //want to show with sort and filter within your query
        return Chamadas.find({medico: 1},{sort: {data_inicio: -1}}).fetch();
      }
});

Alternatively, you’re pub/sub logic might be wrong and you may not actually have any Chamadas on the client. Try typing Chamadas.find().fetch() in the client console and make sure there are actually documents in your minimongo client-side db.

1 Like

It fiixed my issue, thanks man