Contacts array from Cordova function not passing as helper

Hi, so I am making an invite system for my Meteor app, the goal is to choose your friends from your contacts list and send them an SMS text with the link to the app, but I am having trouble passing the contacts from the Cordova plugin “contacts” into my blaze template, here is what I am using:

Template.invitarAmigos.helpers({
  contactos: function () {
    if(Meteor.isCordova){
      function onSuccess(contacts){
        console.log('Found ' + contacts.length + ' contacts.');
        console.log(contacts);
        return contacts;
      };
      function onError(contactError){
        return null;
      };
      var options = new ContactFindOptions();
      options.multiple = true;
      var fields       = ["displayName", "name"];
      navigator.contacts.find(fields, onSuccess, onError, options);
    }
  }
});

the function is working because I can see the log in Xcode, but when I try to display them nothing appears:

<template name="invitarAmigos">
      {{#ionList}} 
        {{#if contactos}}
          Sí hay contactos
        {{/if}}
        {{#each contactos}}
            {{#ionItem}}
                  Contacto
            {{/ionItem}}
        {{/each}}
      {{/ionList}}
</template>

Do anybody knows what is wrong here? Thanks in advance!

Ok, so I fixed this doing the find of the contacts on the .render function and assigning a value to a variable Session which got returned on the helper function, hope this might help somebody. :wink:

2 Likes

Good to hear that you managed to get it to work! Would you be willing to share the code that you used? Trying to do pretty much the same thing right now :grin:

Sure, at the end I dropped this feature, but this is how I got it to work:

Template.invitarAmigos.rendered = function () {
    var ayudaContactos = [
    if(Meteor.isCordova){
      function onSuccess(contacts){
        console.log(contacts);
        contacts = _.sortBy(contacts, function(o) { return o.name.givenName; })
        Session.set("contactos",contacts);
      };
      function onError(contactError){
        Session.set("contactos","");
      };
      var options = new ContactFindOptions();
      options.multiple = true;
      var fields       = ["displayName", "name"];
      var contactos = navigator.contacts.find(fields, onSuccess, onError, options);
    }else{
      Session.set("contactos", ayudaContactos);
    }

    
};


Template.invitarAmigos.helpers({
  contactos: function () {
    return Session.get("contactos");
  },
  tituloNav: "Invitar amigos"
});

Hope this little snippet helps you out. :wink:

1 Like

Fantastic. Worked a charm. Thanks a million! :grin:

Hi @cosio55

How did you add the Cordova contacts plugin in the first place?
Is this it?

meteor add cordova:org.apache.cordova.contacts@1.1.0

I get the message:

Added Cordova plugin cordova-plugin-contacts@1.1.0 (plugin has been renamed as
part of moving to npm).

Could you give me the command you used?
I would be very grateful for it.

Thanks
Brad

I think that what this is telling you is that you have the correct package, but the named changed as you can see in the documentation of the Cordova plugin here: https://www.npmjs.com/package/cordova-plugin-contacts