2015-10-12 112 views
0

對不起,如果我的問題是太容易了,我開始用流星Meteorjs aldeed流星表格與publishcomposite

我試圖使用aldeed:表格,但不是爲我工作。 我有以下的配置,但是以標題 「Ingrediente」 「更新者」 列,都是空的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 }); 
      } 
     } 
    ] 
    } 
}); 

/應用/客戶/模板/ preco_ingredientes/preco_list/preco_ingredientes_lis t_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

我從PrecoIngrediente整個的Json直接從MongoDB的

{ 
    "_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

我們可以從

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

也將是不確定的,並與

user:undefined 
+0

感謝米歇爾

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

將失敗。從PrecoIngrediente整個JSON是:{ 「_id」: 「g2NMe7DwCxPQWivD9」, 「quantidade」:123, 「PRECO」:1111.22, 「ingredienteId」: 「」, 「createdAt」:ISODate(「2015 - 10-01T20:26:21。647Z「), 」updatedAt「:ISODate(」2015-10-01T20:26:21.647Z「), 」updatedById「:」XBGiQDNdW25JdMHew「 }我直接從MongoDB獲得 – user2543118

+0

但是仍然沒有'userId'鍵 –

+0

謝謝@ Michel-floyd,我沒有看到我的錯誤,我在我的preco_ingredientes_controller.js中修正了「updatedById」,現在和我的MongoDB對象一樣,但是字段「 – user2543118