Meteor:使用Meteor.methods()的内部服务器错误[500]

时间:2021-12-15 16:45:40

I have the collection "menu" with structure:

我有结构的“菜单”集合:

{
"_id" : "vJQr7EuieDHMuX2nx" "section" : "vine" "price" : "200" "category" : "bar"
"_id" : "vJQr7EuieDHMuX4rf" "section" : "beer" "price" : "200" "category" : "kitchen"
"_id" : "vJQr7EuieDHMuX5tg" "section" : "milk" "price" : "200" "category" : "kbar"
"_id" : "vJQr7EuieDHMuX4sc" "section" : "beer" "price" : "200" "category" : "kitchen"
"_id" : "vJQr7EuieDHMuX2xs" "section" : "vine" "price" : "200" "category" : "bar"
}

I want to show the user only "section" without repeating For this I used Menu.aggregate([{$group:{_id:"$section"}}]) but it's not working How can I do this? May be to use a different method?

我想向用户显示“section”而不重复为此我使用了Menu.aggregate([{$ group:{_ id:“$ section”}}])但它不起作用我该怎么做?可能是用不同的方法?

Server side:

Meteor.methods({
  'getSections': function () {
    var pipeline = [{$group:{_id:"$section"}}];
    var result = Menu.aggregate(pipeline);
    var sections = [];
    for (i = 0; i < result.length; i++) {
      sections.push(result[i]._id);
    };
    console.log(sections);
    return sections;
  }
});

result in the server: [ 'vine', 'beer', 'milk' ]

导致服务器:['vine','beer','milk']

Client side:

 Template.MobBarMenu.helpers({
        'list': function(){
            this.render('MobBarMenu', {});
        },
        'listSections': function () {
           Meteor.call('getSections', function (err, data) {
               console.log(data);
               // return data;
           });
        }
    });

template name="MobBarMenu"
            {{#each listSections}}
                {{this}}
            {{/each}}
template

I don't know where to find the solution

我不知道在哪里可以找到解决方案

2 个解决方案

#1


0  

You probably want an advanced publication. On your template code, add a named collection (only on client). Then you can create a helper to get the data and subscribe to the publication.

您可能想要一个高级出版物。在模板代码中,添加命名集合(仅在客户端上)。然后,您可以创建一个帮助程序来获取数据并订阅发布。

const Sections = new Mongo.Collection('sections');
Template.Menu.onCreated(function () {
  this.subscribe('sections')
})
Template.Menu.helpers({
  sections() {
    return Sections.find();
  }
});

Then, create a publication that writes to this.

然后,创建一个写入此的发布。

Meteor.publish('sections', function () {
  const handle = Menu.find({}, {
    fields: {
      _id: 0
      section: 1
   }
  }).observe({
    added: doc => this.added('sections', doc.section, doc),
    changed: doc => this.changed('sections', doc.section, doc),
    removed: doc => this.removed('sections', doc.section, doc)
  });
  this.ready();
  this.onStop(() => handle.stop())
})

#2


0  

Meteor uses MiniMongo - it's own wrapper of nodejs MongoDB driver.

Meteor使用MiniMongo - 它是nodejs MongoDB驱动程序的自己的包装器。

And by default collecions has no method aggregate. You can find it in a few packages, like monbro:mongodb-mapreduce-aggregation.

默认情况下,collecions没有方法聚合。您可以在一些软件包中找到它,例如monbro:mongodb-mapreduce-aggregation。

Or (which better imo) you can get access to raw collection with native to Meteor rawCollection method:

或者(更好的imo)你可以使用Meteor原始的rawCollection方法访问原始集合:

Menu.rawCollection().aggregate(pipeline, function(err, res) {
    //do stuff here
})

#1


0  

You probably want an advanced publication. On your template code, add a named collection (only on client). Then you can create a helper to get the data and subscribe to the publication.

您可能想要一个高级出版物。在模板代码中,添加命名集合(仅在客户端上)。然后,您可以创建一个帮助程序来获取数据并订阅发布。

const Sections = new Mongo.Collection('sections');
Template.Menu.onCreated(function () {
  this.subscribe('sections')
})
Template.Menu.helpers({
  sections() {
    return Sections.find();
  }
});

Then, create a publication that writes to this.

然后,创建一个写入此的发布。

Meteor.publish('sections', function () {
  const handle = Menu.find({}, {
    fields: {
      _id: 0
      section: 1
   }
  }).observe({
    added: doc => this.added('sections', doc.section, doc),
    changed: doc => this.changed('sections', doc.section, doc),
    removed: doc => this.removed('sections', doc.section, doc)
  });
  this.ready();
  this.onStop(() => handle.stop())
})

#2


0  

Meteor uses MiniMongo - it's own wrapper of nodejs MongoDB driver.

Meteor使用MiniMongo - 它是nodejs MongoDB驱动程序的自己的包装器。

And by default collecions has no method aggregate. You can find it in a few packages, like monbro:mongodb-mapreduce-aggregation.

默认情况下,collecions没有方法聚合。您可以在一些软件包中找到它,例如monbro:mongodb-mapreduce-aggregation。

Or (which better imo) you can get access to raw collection with native to Meteor rawCollection method:

或者(更好的imo)你可以使用Meteor原始的rawCollection方法访问原始集合:

Menu.rawCollection().aggregate(pipeline, function(err, res) {
    //do stuff here
})