返回游标数组不会显示任何结果 [英] returning array of cursors does not show any result

查看:71
本文介绍了返回游标数组不会显示任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过这样做来解决两个集合的加入:

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];
    }

我的html字段中的结果只是空白字段而没有错误消息所以是什么我做得对吗?

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

这一点是使用typeId中的_id数字来获取以html格式显示的用户的正确类型。

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.


返回usf; //这将返回所有没有类型的用户

return usf; // this returns all users without types

返回tyf; //这将返回没有用户的所有类型

return tyf; // this returns all types without users

更新1:

模板如下所示:

<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应该找到名称等用户数据。
us_ut_name应该显示我们从us_ut_type获得的ObjectID所获得的类型的名称。

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.

更新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.

打印例如:


john,barber

john, barber

mike ,木匠

降压,狗

在控制台中,但不在网站上的输入字段。

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

解决方案:

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;
        }
    });

谢谢David Weldon

Thank you David Weldon

推荐答案

如果我理解你的模型,这可能有效:

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;
}

它使用给定的选择器获取用户,然后通过添加修改每个用户一个 tyf_type 属性。唯一的缺点是它使用 fetch ,每次用户更改时都会重绘整个列表。

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.

这篇关于返回游标数组不会显示任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆