[SOLVED] [Angular-Meteor] findOne() inside a helper of the other collection

I have already added the package:

  • dburles:collection-helpers

My question is:
why a findOne(); inside a helper of the other collection don’t work?

  • The first console.log(); it works! but it bring me all catalog, and, I don’t want read all values to obtain that I need it, because is not optimum, because I save the ID’s in my collection vacantes, and not the descriptions of the catalogs.
  • The second console.log(); I want obtain the description from a catalog, and I need findOne(); but it sendme undefined

Next my codes:

///// collection.js

import {Mongo} from 'meteor/mongo';
import {Puestos} from '../puestos/collection';

export const Vacantes = new Mongo.Collection('vacantes', {});

Vacantes.deny({
   insert() {return true;},
   update() {return true;},
   remove() {return true;},
});

Vacantes.helpers({
   puestoDesc(){
        // Works! but, obtaining everything
       console.log('Helper description of the puesto', Puestos.find({_id: this.puestoId}));  
       // this is undifined why?
       console.log('Helper description of the puesto', Puestos.findOne({_id: this.puestoId}));
       return 'Here I want return a object property' this.puestoId;
   }
});

///// html
This is my client html code, it describes how that I invoke it:
In the client code: ng-init="puestoDesc = vacante.puestoDesc(); the helper from collection vacantes run!, but the findOne(); is not obtained nothing.


<li class="list-group-item"
   ng-repeat="postulacion in postEnproceso.misPostulaciones"
   ng-init="vacante = postEnproceso.verVacante(postulacion.vacanteId)">
   <div class="row">
      ...some code
   </div>
   <h5 class="list-group-item-heading" ng-init="puestoDesc = vacante.puestoDesc()">
       Postulada para - {{cadenaDesc}} ({{vacante.sucursal}})
       Puesto: {{puestoDesc}}, Marca: {{vacante.marca}},
       Estado: {{estadoDesc}} {{vacante.sueldo | currency}} x día
   </h5>
   <p class="list-group-item-text">
       Publicaca: {{vacante.fechaCreacion | date : format : dd/MM/YYYY}} -
       Postulada: {{postulacion.fechaPostulacion | date : format : dd/MM/YYYY}}
   </p>
</li>

///// js from my client

.. imports
.. imports

class PostEnproceso {

   constructor($scope, $reactive) {
       'ngInject';
       $reactive(this).attach($scope);
       this.subscribe('postulaciones.mis',  ()=> [{estado:1}]);
       this.helpers({
           misPostulaciones() {
               return Postulaciones.find();
           }
       });

   }

   verVacante(id){
       console.log('verVacante', id);
       return Vacantes.findOne({_id:id});
   }
}

const name = 'postEnproceso';

// Módulo
export default angular
   .module(name, [
       angularMeteor,
       uiRouter
   ])
   .component(name, {
       templateUrl: `imports/ui/components/misPostulaciones/${name}/${name}.html`,
       controllerAs: name,
       controller: PostEnproceso
   })
   .config(config);

Hello everyone, thanks for read

I solved the problem, I don’t know if this is the best way, so any feedback will be welcome.

This was the actions to implement:

  • In the publishComposite I add a children that search the element from my catalog.
  • I add a helper() in the ‘vacantes’ collection.
  • In the html I use the ng-init to call that helper(), which already brings me the desired value of each.

Next my codes:

/////// server side ///////

Meteor.publishComposite('postulaciones.mis', function(estado) {
    if (!this.userId) return;
    const candidato = Candidatos.findOne({
        propietario: this.userId
    });
    const selector = {
        $and: [{
            candidatoId: candidato._id
        }, estado]
    };

    return {
        find: function() {
            return Postulaciones.find(selector, {
                fields: {
                    fechaCreacion: 0,
                }
            });
        },
        children: [{
            find: function(postulacion) {
                return Vacantes.find({
                    _id: postulacion.vacanteId
                }, {
                    fields: {
                        _id: 1,
                        fechaCreacion: 1,
                        cadenaId: 1,
                        marca: 1,
                        sucursal: 1,
                        estadoId: 1,
                        sueldo: 1,
                        puestoId: 1
                    }
                });
            },
            children: [{
              ////////////////////////////////////////////////////////////////////////////////////////
              // I add this children (consult), to obtain from my catalog the corresponding value   //
              ////////////////////////////////////////////////////////////////////////////////////////
                find: function(vacante) {
                    return Puestos.find({
                        _id: vacante.puestoId
                    });
                }
            }]
        }]
    };
});

/////// collection.js ///////

Vacantes.helpers({
    puestoDesc() {
        return Puestos.findOne({
            _id: this.puestoId
        });
    }
});

/////// file.html ///////

<li class="list-group-item"
   ng-repeat="postulacion in postEnproceso.misPostulaciones"
   ng-init="vacante = postEnproceso.verVacante(postulacion.vacanteId)">
   <div class="row">
     .. more code
   </div>
   <h5 class="list-group-item-heading" ng-init="puestoDesc = vacante.puestoDesc()">
       Postulada para - {{cadenaDesc}} ({{vacante.sucursal}})
       Puesto: {{puestoDesc}}, Marca: {{vacante.marca}},
       Estado: {{estadoDesc}} {{vacante.sueldo | currency}} x día
   </h5>
   <p class="list-group-item-text">
       Publicaca: {{vacante.fechaCreacion | date : format : dd/MM/YYYY}} -
       Postulada: {{postulacion.fechaPostulacion | date : format : dd/MM/YYYY}}
   </p>
</li>

/////// file.js ///////

class PostEnproceso {
    constructor($scope, $reactive) {
        'ngInject';
        $reactive(this).attach($scope);
        this.subscribe('postulaciones.mis', () => [{
            estado: 1
        }]);
        this.helpers({
            misPostulaciones() {
                return Postulaciones.find();
            }
        });
    }

    verVacante(id) {
        return Vacantes.findOne({
            _id: id
        });
    }
}

Thanks for your atention

Greetings