【问题标题】:Meteor: Meteor.call() from within observe callback does not executeMeteor:来自观察回调的 Meteor.call() 不执行
【发布时间】:2013-09-09 19:40:11
【问题描述】:

是否有可能从 Meteor 的 observe 回调中调用服务器方法?

我整理了一个重现该问题的示例,即从 myCursor.observe() 的回调中调用的 Meteor.call() 不会执行。当从观察回调中调用时,Meteor.method 本身也不会出现错误回调,它只是返回 Undefined

不要理我,Meteor.call() :) 非常感谢任何帮助!

observe.js

items=new Meteor.Collection("Items");

if (Meteor.isClient) {
    Meteor.subscribe("Items");
    Meteor.startup(function(){
        itemsCursor=items.find();
        itemsHandle=itemsCursor.observe({
            added : function(doc){
                console.log("added "+doc.text);
                Meteor.call('aMethod',doc.text,function(e,r){
                    if(e){
                        console.log("error from server: "+e);
                    }else{
                        console.log("response from server: "+r);
                    }
                });
            },
            removed : function(doc){
                console.log("removed "+doc.text);
                Meteor.call('aMethod',doc.text,function(e,r){
                    if(e){
                        console.log("error from server: "+e);
                    }else{
                        console.log("response from server: "+r);
                    }
                });
            }
        });
    });

    Template.test.items=function(){
        return items.find();
    }
    Template.test.events({
        'click #add':function(){
            items.insert({"text":"Timestamp: "+(new Date().getTime())});
        },
        'click #remove':function(){
            items.remove(items.findOne()._id);
        }
    });
}

if (Meteor.isServer) {
    Meteor.publish("Items",function(){
        return items.find();
    });
    items.allow({
        insert : function(userId,doc){
            return true;
        },
        update : function(userId,doc){
            return true;
        },
        remove : function(userId,doc){
            return true;
        }
    });
    Meteor.methods({
        aMethod:function(text){
            console.log("Got it! "+text);
            return "Got it! "+text;
        }
    });
}

observe.html

<head>
  <title>observe</title>
</head>

<body>
  {{> test}}
</body>

<template name="test">
  <button id="add">add item</button>
  <button id="remove">remove item</button>
    <ol>
        {{#each items}}
        <li>{{text}}</li>
        {{/each}}
    </ol>
</template>

【问题讨论】:

    标签: javascript mongodb collections meteor


    【解决方案1】:

    这可能是一个已知问题,我不确定,因为我自己没有尝试过,但看起来可能有解决方法(请参阅https://github.com/meteor/meteor/issues/907

    将您的 Meteor.call 添加到瞬时 setTimeout 回调中:

    added: function(doc) {
        console.log("added "+doc.text);
        setTimeout(function() {
            Meteor.call('aMethod',doc.text,function(e,r){
                if(e){
                    console.log("error from server: "+e);
                }else{
                    console.log("response from server: "+r);
                }
            });
        },0);
    }
    

    【讨论】:

    • 添加它并且它有效。谢谢!很抱歉忽略了一个已知问题。
    • 也称为Meteor.defer... ?
    【解决方案2】:

    顺便说一句,Tarang's answer 在从客户端方法调用时也有效。本质上,我在客户端范围内有一个文件。这个文件是一个方法定义。在我的方法定义中,我在服务器上调用了一个方法。

    我遇到了问题,因为只有服务器调用的客户端存根被调用,而服务器方法没有被调用。修复是这样的:

    Meteor.methods({  
        assignUserSession: function(options) {
            setTimeout(function() { // https://stackoverflow.com/questions/18645334/meteor-meteor-call-from-within-observe-callback-does-not-execute
                // Find all the base_accounts of this user and from there decide which view to prioritize.
                Meteor.call('user_base_accounts', {user_id: Meteor.userId()}, function(error,result){
                    ....
                    //server is then called due to logs shown in the command prompt
                    ....
                });
            });
        }
    });
    

    感谢您指出这一点!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      相关资源
      最近更新 更多