遍历 mongo 光标以列出 Meteor Blaze 中的所有值

Iterate through mongo cursor to list all values in Meteor Blaze?

本文关键字:Blaze Meteor 光标 mongo 遍历      更新时间:2023-09-26

如果有人能指出我正确的方向,我将不胜感激!!我有一个应用程序将 csv 上传到 mongo,然后在 meteor 中发布。在 Template.onCreated 中,我正在订阅它,在 Template.helper 中,我正在尝试遍历 mongo 光标以获取要在表中显示的值。

Template.table.onCreated(function() {
  Template.instance().subscribe('contacts');
});
Template.table.helpers({
  contact() {
    var contactCursor = Contacts.find();
    return contactCursor;
  }
});

我尝试了 cursor.fetch() 和地图,但没有渲染或 chrome 崩溃。我的表如下所示:

    <tr>
      <td>
        {{#each contact}}
          {{contact}}
        {{/each}}
      </td>
    </tr>

我能够在表格中渲染的只是

[object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] [object Object]

或者什么都没有。如果有人可以帮助我,我是流星/火焰的新手,无法弄清楚如何遍历光标。mongo 集合如下所示:

{ "_id" : "Mzb6a9uh3948vw", "contact" : [ { "emailAddress" : "glen@example.com", "someContact" : "No", "creationDate" : "N/A", "bounceBack" : "N/A", "unsubscribed" : "N/A" } ] }

我想在表格中做这样的事情:

        {{#each cursor}}
          {{emailAddress}}
          {{someContact}}
          {{createdDate}}
          {{bounceBack}}
          {{unsubscribed}}
        {{/each}}

谢谢

尝试:

//helper
contacts() {
    var contactCursor = Contacts.find();
    return contactCursor;
}
//html
{{#each contacts}} 
    {{#each contact}}
        <p>email: {{emailAddress}</p>
    {{/each}}
{{/each}}

另外,为什么你必须拥有它

{ "_id" : "Mzb6a9uh3948vw", 
"contact" : [ { "emailAddress" : "glen@example.com", "someContact" : "No", "creationDate" : "N/A", "bounceBack" : "N/A", "unsubscribed" : "N/A" } ] 
}

而不是:

"_id" : "Mzb6a9uh3948vw", 
"emailAddress" : "glen@example.com", 
"someContact" : "No",
//the rest

然后你可以像{{emailAddress}} //returns "glen@example.com"一样使用它们,{{someContact}} //returns "No"

编辑

您可以将联系人单独插入到联系人集合中,并通过为每个联系人保存相关 ID 来返回联系人。假设它的一个用户的联系人。您将 userId 保存在插入中,然后返回如下内容:

Contacts.find({userId: Meteor.userId()});

您也可以从您的出版物中做到这一点,我认为在大多数情况下更好。

您的contact帮助程序返回一个游标,其对象嵌套一个名称也为 contact 的数组。您还必须遍历该数组:

{{#each contact}}
  id: {{_id}}
  {{#each this.contact}}
    email address: {{emailAddress}}
    some contact?: {{someContact}}
    creation date: {{creationdate}}
    bounce back: {{bounceBack}}
    unsubscribed?: {{unsubscribed}}
  {{/each}}
{{/each}}