【问题标题】:Meteor.methods fails on the server sideMeteor.methods 在服务器端失败
【发布时间】:2015-03-20 23:53:28
【问题描述】:

我的目标是将设备令牌插入数据库。该方法在客户端成功,但在服务器端失败。我不知道为什么。

// lib/meteor_methods.js

Meteor.methods({
  savePushTokens: function(myToken) {
    console.log("Saving the token.")
    Tokens.insert({token: myToken}, function(error, result) {
        if (error) {
            console.log(error);
        }
        else {
            console.log(result);
        }
    });
    console.log("Token is now saved!");
  }
})

// client/push_notifications.js

...
function tokenSuccessHandler(result) {
  console.log('token success result ' + result);
  window.localStorage.setItem('deviceToken', result);
  // API call to store token on your DB
  Meteor.call('savePushTokens', result)
  console.log(Tokens.find().fetch());
}
...

请查看整个文件:https://github.com/mvaisanen/SimplePushApp/blob/pushplugin/client/push_notifications.js

Xcode 控制台输出

2015-01-22 10:27:51.165 myapp[33366:5680153] token success result 
077f2ea72eb6b2dfc381ce27f2eb12e2ee8ee68f7eeb90f7f2f10f1d99cd140e
2015-01-22 10:27:51.166 myapp[33366:5680153] Saving the token.
2015-01-22 10:27:51.166 myapp[33366:5680153] zp6vkrN5M4HtKF9NF
2015-01-22 10:27:51.166 myapp[33366:5680153] Token is now saved!
2015-01-22 10:27:51.166 myapp[33366:5680153] [{"token":"077f2ea72eb6b2dfc381ce27f2eb12e2ee8ee68f7eeb90f7f2f10f1d99cd140e","_id":"zp6vkrN5M4HtKF9NF"}]

一切似乎都很好。

但是,如果我在流星外壳、客户端或服务器中查询数据库,结果是空集合。 (> Tokens.find().fetch() [])。

如果我在服务器端运行该方法,它实际上永远不会运行。见下文。

// lib/meteor_methods.js

Meteor.methods({
    savePushTokens: function(myToken) {
        console.log("Saving the token.")
        if (Meteor.isServer) {
                Tokens.insert({token: myToken}, function(error, result) {
                if (error) {
                    console.log(error);
                }
                else {
                    console.log(result);
                }
            });
            console.log("Token is now saved!");
        }
    }
});

Xcode 控制台输出

2015-01-22 10:32:59.290 myapp[33375:5681416] token success result 077f2ea72eb6b2dfc381ce27f2eb12e2ee8ee68f7eeb90f7f2f10f1d99cd140e
2015-01-22 10:32:59.291 myapp[33375:5681416] Saving the token.
2015-01-22 10:32:59.291 myapp[33375:5681416] []

有什么想法吗?完整的 repo 在这里https://github.com/mvaisanen/SimplePushApp/tree/pushplugin

更新

我安装了autopublishinsecure

我使用meteor run ios-device --mobile-server 192.168.1.6:3000 启动应用程序。

我可以通过meteor shell或meteor mongo手动将数据插入数据库。

我也尝试在meteor mongo 中检查令牌:

$ meteor mongo
MongoDB shell version: 2.4.12                 
connecting to: 127.0.0.1:3001/meteor
meteor:PRIMARY> db.tokens.find();
meteor:PRIMARY>

但我没有得到任何令牌。

更新 2

我无法修复错误。 github(或其他地方)上是否有任何简单(或复杂)的 Meteor 应用程序具有 Apple 推送通知,我可以克隆这些应用程序从而找到解决方案。

【问题讨论】:

  • 基本问题...您是否安装/删除了“不安全”和“自动发布”软件包?如果它们被删除,您需要发布和订阅数据并拥有对集合的写入权限。现在您正在成功写入客户端 minimongo。
  • “不安全”和“自动发布”都已安装。我有带有推送通知的非常基本的应用程序。我只是在测试我是否可以让推送通知正常工作。
  • 您是否安装了aldeed:simpleschemaaldeed:collection2
  • @richsilv,不。实际上从未听说过。

标签: mongodb cordova meteor apple-push-notifications


【解决方案1】:

令牌很可能正在保存,但是您查看它的方式使它看起来好像不是,因为它可能在您正在查看的地方不可见立即看到它。

请记住:

  • Meteor 不会将所有数据发布到客户端,除非您的项目中有 autopublish
  • 如果你在服务器上插入一些东西,它不会立即在客户端上可用,如果它发布到客户端的话

您在调用Meteor.call 之后查找令牌的方式是,此时客户端不太可能收到数据。

我不确定这是否会有所不同,但您在服务器上使用了异步 javascript。 Meteor 使用纤维,您不必这样做。

这很有帮助,如果有问题,meteor 会抛出错误。你可以这样做:

if (Meteor.isServer) {
    Tokens.insert({token: myToken});
    console.log("Token is now saved!");
}

要检查是否已插入令牌,最好的方法是在您的应用程序运行时使用meteor mongo 控制台并检查令牌是否在其中,这是数据库的实际 mongo shell,因此流星的发布延迟/缺少发布方法可能不是问题。

meteor mongo
>db.tokens.find();

(如果您的收藏被称为“令牌”,即:var PushTokens = new Mongo.Collection("tokens")

【讨论】:

  • 如果我在流星 mongo 中检查令牌,它不会返回任何内容。 lib文件夹是放置方法的正确位置吗?我是否应该将 Meteor.isServer 与方法一起使用?目前这两种方法都行不通。
猜你喜欢
  • 2019-06-26
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
  • 2018-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多