Gravity123
Gravity123

Reputation: 1130

returning array of cursors does not show any result

im trying to solve a "join" of two collections by doing like this:

foundUsers: function()
    {
        var searchUser = Session.get("searchUser"); // user search criteria
        var usf = User.find(searchUser, {}); // get user results
        
        var typeId = usf.map(function(p) { return p.us_ut_id }); // get type ids
        
        var tyf = Type.find({_id: {$in: typeId}}, {}); // new Meteor.Collection.ObjectID("533d63bef0e236f9d76db905")
        
        var typeName = tyf.map(function(p) { return p.ut_name.toString() }); // check type names
        console.log(typeName);
        
        return [usf, tyf];
    }

The result in my html fields are just blank fields and no error message so what am I not doing right?

The point of this is to use the _id numbers in typeId to get the correct type of a user displayed in a html form.

return usf; // this returns all users without types

return tyf; // this returns all types without users

UPDATE 1:

A template would look like this:

<template name="getResult">
    {{#each foundUsers}}
        <input type="text" id="firstname" value="{{us_firstname}}"/><br>
        <input type="text" id="lastname" value="{{us_lastname}}"/><br>
        <input type="text" id="typename" value="{{us_ut_name}}"/><br>
    {{/each}}
</template>

us_xxx should find the user data such as names. us_ut_name should display the name of the type which we get by the ObjectID we got from us_ut_type.

UPDATE 2:

So I made my code like this which I believe should work since it prints the right data in the console:

// display user data
    Template.getResult.helpers(
    {
        foundUsers: function()
        {
            var selector = Session.get('searchUser');
            var users = User.find(selector).fetch();
            
            return _.each(users, function(user)
            {
                console.log(user);
                var result = user.us_ut_name = Type.findOne({_id: new Meteor.Collection.ObjectID(user.us_ut_id._str)}).ut_name;
                console.log(user.us_firstname + ", " + result);
                return result;
            });
        }
    });

But the result is not shown on the page.

this prints for example:

john, barber

mike, carpenter

buck, dog

in the console, but not in the input fields on the website.

SOLUTION:

Template.getResult.helpers(
    {
        foundUsers: function()
        {
            var selector = Session.get('searchUser');
            var users = User.find(selector).fetch();

            _.each(users, function(user)
            {
                console.log(user);
                var result = user.us_ut_name = Type.findOne({_id: new Meteor.Collection.ObjectID(user.us_ut_id._str)}).ut_name;
                console.log(user.us_firstname + ", " + result);
                return result;
            });

            return users;
        }
    });

Thank you David Weldon

Upvotes: 2

Views: 104

Answers (1)

David Weldon
David Weldon

Reputation: 64342

If I understand your models correctly, this may work:

foundUsers: function() {
  var selector = Session.get('searchUser');
  var users = User.find(selector).fetch();
  _.each(users, function(user) {
    return user.tyf_type = Type.findOne(user.us_ut_id).ut_name.toString();
  });
  return users;
}

It fetches the users with the given selector and then modifies each of them by adding a tyf_type property. The only downside of this is that it uses fetch which will redraw the whole list every time any user changes.

Upvotes: 1

Related Questions