Meteorjs使用publishcomposite来表示流星表

时间:2015-10-12 00:38:43

标签: meteor

对不起,如果我的问题太容易了,我将从Meteor开始

我正在尝试使用aldeed:tabular,但不适合我。 我有以下配置,但标题为“Ingrediente”,“Updated By”的列在DataDatable中为空

screenshot

/app/lib/controllers/preco_ingredientes_controller.js

TabularTables = {};

Meteor.isClient && Template.registerHelper('TabularTables', TabularTables);

PrecoIngredientes.helpers({
  ingrediente: function () {
    console.log("PrecoIngredientes.helpers this.ingredienteId:" + JSON.stringify(this));
    return Ingredientes.find(this.ingredienteId).name;
    // return Ingrediente;
  },
  updated: function () {
    var user = Meteor.users.findOne({_id: this.updatedById});
    console.log("user:" + JSON.stringify(user));
    return user && user.name;
    // return Ingrediente;
  },

});

TabularTables.PrecoIngredientes = new Tabular.Table({
  name: "PrecoIngredientes",
  collection: PrecoIngredientes,
  pub: "precoIngredientes_Composite",
  columns: [
    {data: "quantidade", title: "Quantidade"},
    {data: "preco", title: "Preco"},
    {data: "ingrediente()", title: "Ingrediente"},
    {data: "updated()", title: "Updated By"},

  ]
});

/app/server/publish.js

Meteor.publishComposite('precoIngredientes_Composite',function (tableName, ids, fields) {
    check(tableName, String);
    check(ids, Array);
    check(fields, Match.Optional(Object));

    this.unblock(); // requires meteorhacks:unblock package
    return {
    find: function() {
       this.unblock(); // requires meteorhacks:unblock package
       console.log("Publish:" + JSON.stringify(PrecoIngredientes.findOne()));
        return PrecoIngredientes.find({});
    },  
    children: [ 
        {
            find: function(precoIngrediente) {
              this.unblock(); 
                return Meteor.users.find(
                    { _id: precoIngrediente.updatedById },
                    { limit: 1});
            }
        },
        {
            find: function(precoIngrediente) {

              // console.log("child2:");
              this.unblock(); 
                return Ingredientes.find({ _id: precoIngrediente.ingredienteId });
            }
        }
    ]
  }
});

/app/client/templates/preco_ingredientes/preco_list/preco_ingredientes_list_new.html

{{> tabular table=TabularTables.PrecoIngredientes class="table table-striped table-bordered table-condensed"}}

/app/lib/collections/preco_ingredientes.js

PrecoIngredientes = new Mongo.Collection('precoIngredientes');

var schemasPrecoIngrediente = new SimpleSchema({
    ...,
    ingredienteId: {
    label: "Ingrediente",
    type:  String,
    optional: true,

    autoValue: function() {
      if ( this.isSet ){
        return this.value;
      } else {
        return '';
      }
    },
    autoform: {
      afFieldInput:{
        placeholder: 'Select One'
      },
      label: "Ingrediente",
      selectOnBlur: true,
      type: "select2",
      options: function() {
       var list = [];
       list.push({label: "", value: ""});
        var ingredientesList = Ingredientes.find();
        ingredientesList.map(function(ingrediente) {
          list.push({
            label: ingrediente.name,
            value: ingrediente._id
          });
        });
        return list;
      }
    }
  },
    updatedById: {
        type: String,
        label: "Updated by",
        autoValue: function() {
            if (!this.value)
                return this.userId;
        },
        optional: true
    },
...
});

Chrome控制台

    PrecoIngredientes.helpers this.ingredienteId:{"preco":79.78,"quantidade":123,"_id":"FWuxM5wKE7969kkMk"}
user:undefined
PrecoIngredientes.helpers this.ingredienteId:{"preco":1111.22,"quantidade":123,"_id":"g2NMe7DwCxPQWivD9"}
user:undefined

我做错了什么?

谢谢

EDIT1

我直接从MongoDB获得了PrecoIngrediente的整个Json

    {
    "_id" : "FWuxM5wKE7969kkMk",
    "ingredienteId" : "kwqRCm8kaNCofmPqN",
    "quantidade" : 123,
    "preco" : 79.78,
    "createdAt" : ISODate("2015-10-01T20:24:38.304Z"),
    "updatedAt" : ISODate("2015-10-01T20:24:38.304Z"),
    "updatedById" : "XBGiQDNdW25JdMHew"
}

EDIT2

我添加了一个登录publishcomposite

服务器日志:

I20151012-11:18:31.611(-3)? Publish: {"_id":"FWuxM5wKE7969kkMk", "ingredienteId":"kwqRCm8kaNCofmPqN","quantidade":123,"preco":79.78,"createdAt":"2015-10-01T20:24:38.304Z","updatedAt":"2015-10-01T20:24:38.304Z","updatedById":"XBGiQDNdW25JdMHew"}

1 个答案:

答案 0 :(得分:1)

我们可以从您的控制台输出中看到

console.log("PrecoIngredientes.helpers this.ingredienteId:" + JSON.stringify(this));

this的值为:

{"preco":1111.22,"quantidade":123,"_id":"g2NMe7DwCxPQWivD9"}

但是在你的updated帮助器中,你指的是this.userId,它将是未定义的。因此

var user = Meteor.users.findOne({_id: this.userId});

也将是未定义的

console.log("user:" + JSON.stringify(user));

将失败

user:undefined