[SOLVED]Connecting users mongo strategy

Hello,

I’m a bit of a conundrum (am I doing it right?), I want to ‘connect’ users and it doesn’t seem to work so far.

The idea is that each user has a set of contacts, now I want to search the db for users with that phone number to see if I already have users that are using the application.

When I find a user matching, I add a small .hasApp bool true in the list where I did this from, and for the user that I found, I’m still going to add the USER that has searched for him as a friend.

Now, this is the logic, but it doesn’t come up right :).

I’m trying something like this but it doesn’t work at all

for (var i = 0;i < contacts.length; i++) {
            Meteor.users.find({"profile.phone": contacts[i].phone}).forEach(function(friend){
                console.log('We found a friend', friend);
                contacts[i].appActive = true;
                contacts[i].appId = friend._id;

                Meteor.users.update({_id: friend._id}, { $addToSet: { "profile.friends": Meteor.userId() }});
            });
        }

Now, I know that I should probably be spanked for the way I am using Mongo, but that’s why I am asking, I’m new at this.

Are Mongo calls asynchronous (they should be), then how do I piggyback on the response (promise, callback etc.) to do further manipulation? :disappointed:

I’m also spending time on mongodb.org, I hope to solve it, but any pointers you could give me are welcomed!

Thanks!

I saw that I could push the result upstream to the client and from there call another method that does this, but this is ugly as hell, these operation are needed strictly on the server side, the end result need to get to the client only.

You could wrap that whole code block in a meteor method and declare it only on the server side.

Therefore when you call the method from the client, your method will have had access to the complete data set on the server.

// server.js

Meteor.methods({
  'connectFriends': function(){
    var contacts = Meteor.user().contacts;
    var friends = 0;
    for (var i = 0;i < contacts.length; i++) {
      Meteor.users.find({"profile.phone": contacts[i].phone}).forEach(function(friend){
        console.log('We found a friend', friend);
        friends++;
        contacts[i].appActive = true;
        contacts[i].appId = friend._id;
        Meteor.users.update({_id: friend._id}, { $addToSet: { "profile.friends": Meteor.userId() }});
      });
    }
    return friends;
  }
});

// client.js

Meteor.call('connectFriends', function(e,r) {
  if (r) {
    console.log("yippie! found and added " + r + " friends!");
  }
});

The code I pasted is already on the server side, inside a method, and is not working, not returning anything, that’s why I asked about the nature of the connection with mongo, async or not.

There must some error in the code somewhere, I’ve been at it since last night and tried countless ways to do this, I missed something somewhere.

the this.unblock at the very beginning tells meteor to not wait at all or
the return value. so if your method returns something and you need that in
your client call’s callback function, remove this.unblock and make sure you
return something.

@serkandurusoy is right when he says that a method needs to return something for it to be made available to the client call, but it is OK to use this.unblock() - see this reply which returns method data to a client. In that example, the client uses a template reactive variable to ensure the view is updated when the data becomes available. Your use case may be simpler.

BTW, the method’s userId parameter is not used in your code snippet (and probably shouldn’t be, since it may allow a client to subvert the method).

Wow! thanks @robfallows I was blondly avoiding this.unblock with methods that need to return a value and you’ve opened my eyes to it! now I need to unlearn that :smile:

I had the same block until a :bulb: moment a few days ago. I had to do a myself.unblock() :laughing:

myself.unblock()

LOL hahaha :slight_smile:

Btw, guys, I’m a huge $ass :).

I am processing the phone numbers beforehand in a service, the number in the contacts I would clean before process ( removing spaces, ( ) and -'s ) and the contact’s number I forgot to clean.

So, the stuff works :). Good I had the inspiration to compare the hashes of the phone numbers (as it it’s not okay to save them on the server.)

1 Like

Thanks very much for the advice!

I learn something helpful every day here :slight_smile: