【发布时间】:2016-04-26 01:19:25
【问题描述】:
不知何故我解决了我的问题。
我没有使用 Meteor.methods,然后我开始使用它们。
我还没有使用订阅,发布东西。
--- 解决方案---
在 server/methods.js 中
if (Meteor.isServer) {
Meteor.methods({
insertDoc: function(someValueToInsert1, someValueToInsert2) {
return Docs.insert({
owner: Meteor.user().username,
type: someValueToInsert1,
files: someValueToInsert2
});
// `return Docs.insert()` function because
// Docs.insert returning the _id value of
// this entry. And i will use the _id value
// at client side.
// For ex.: Insert doc and go to doc page
// Used router like this:
// http:// ... /document/:id/view
// http:// ... /document/:id/edit
// http:// ... /document/:id/general
}
});
}
在客户端/upload.js中
if (Meteor.isClient) {
Template.upload.events({
'click #uploadButton': function(){
// Some variable definitons
// like someValueToInsert1, someValueToInsert2 etc
Meteor.call('insertDoc', someValueToInsert1, someValueToInsert2);
// That's all.
// But you can add your callback your Meteor.call function
// To pass data from server to client like:
Meteor.call('insertDoc', someValueToInsert1, someValueToInsert2, function(error, result) {
if(!error) Router.go('seeUploadedDataByID', { id: result });
});
}
});
}
---我的问题是---
尝试了太多方法。
我的应用:用户可以创建文档。 Docs 是一个集合。
如果用户创建文档,将文档 ID 推送到 Meteor.user().profile.docs。
我做了什么:
方式1:在Router.js中
Router.route("/docs", {
"name": "docs",
data: function(){
var userDocs = Meteor.users.find({_id: thisId}, {fields: {"profile.docs": 1}});;
return Docs.find({_id: {$all: userDocs}});
// console.log(Docs.find({_id: {$all: userDocs}}))
// This console.log returns a weird data, not what i want
// It looks like Mongo function
}
});
方式2:在server/publishs.js中
Meteor.publish("myDocs", function() {
return Meteor.users.find({_id: this.userId}, {fields: {"profile": 1}});
// Actually i wanted to reach userData first
// If i can reach datas, i will try to reach profile.docs
});
然后在client/docs.js中
Template.docs.onCreated(function() {
this.subscribe("myDocs");
});
然后在client/docs.html中
{{#if Template.subscriptionsReady}}
{{#each myDocs}}
{{this.docs}}
{{this.profile}}
{{this}}
{{this[0]}}</div>
{{/each}}
{{else}}
Loading...
{{/if}}
当我渲染时,说正在加载.. 然后什么都没有出现。
方式3:在client/docs.js中
Template.docs.helpers({
myDocs: function(){
var docs = Meteor.user().profile.docs;
var docsArr = [];
for (var i=0;i<docs.length;i++){
var doc = Docs.findOne({_id: docs[i]});
docsArr.push(doc);
}
return docsArr;
}
});
我想在清醒的时候解决这个问题。
问:我应该创建一个新的用户集合并将 docId 推送到其中吗?
【问题讨论】:
标签: meteor