【问题标题】:Meteor: Can't subscribe dataMeteor:无法订阅数据
【发布时间】: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


    【解决方案1】:

    默认情况下,会发布用户名、电子邮件和个人资料。 所以你可以试试这个解决方案:

    路由器.js

    Router.route('/docs', {
        name: 'docs',
        waitOn: function() {
            if(Meteor.user()){
                return Meteor.subscribe('myDocs', Meteor.user().profile.docs);
            }
        }
    });
    

    服务器/publishs.js

    Meteor.publish("myDocs", function(docs) {
        return Docs.find({_id: {$in: docs}});
    });
    

    client/docs.html

    {{#each myDocs}}
        {{title}}
        {{description}}
    {{/each}}
    

    client/docs.js

    Template.docs.helpers({
        myDocs: function(){
            return Docs.find();
        }
    });
    

    【讨论】:

    • 其实我想通了,非常感谢您的关注。我决定使用 Meteor.methods 这让我感觉更安全。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多