Meteor.methods失败,在服务器端 [英] Meteor.methods fails on the server side

查看:230
本文介绍了Meteor.methods失败,在服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My goal is to insert device token into db. The method succeeds on the client but fails on the server side. I have no idea why.

// 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!");
  }
})

//客户端/ 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

X $ C $。c控制台输出

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"}]

似乎一切都很好。

Everything seems fine.

不过,如果我查询里面流星外壳,客户端或服务器的数据库,结果是空集。 (> Tokens.find()取()。
[]
)。

However, if I query database inside meteor shell, client or server, the result is empty collection. (> Tokens.find().fetch() []).

如果我运行在服务器端的方法,它实际上从未运行。见下文。

If I run the method on the server side, it actually never runs. See below.

// 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!");
        }
    }
});

X $ C $。c控制台输出

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] []

你知道吗?完整的回购是在这里 https://github.com/mvaisanen/SimplePushApp/tree/pushplugin

更新

自动发布不安全安装。

我开始与应用流星运行IOS的设备--mobile服务器192.168.1.6:3000

我可以手动插入数据通过流星壳或流星蒙戈为DB。

I can manually insert data to db via meteor shell or 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>

但我没有得到任何标记。

But I get no tokens.

更新2

我一直无法修复的bug。有没有简单(或复杂)应用程式流星github上(或其他地方),其中有苹果推送通知和我可以克隆,从而找到解决方案。

I haven't been able to fix the bug. Is there any simple (or complex) Meteor apps on github (or somewhere else) which have Apple push notifications and which I could clone and thus find the solutions.

推荐答案

它很可能令牌的的节省,但你看它的方式使它看起来好像它不是,因为它可能不是你要找的瞬间,地方可见其正在看着。

Its quite likely that the token is saving but the way you're looking at it makes it appear as if its not, because it may not be visible at the places you're looking at the instant its being looked at.

记住:


  • 流星不会将所有的数据发布到客户端,除非自动发布为您的项目。

  • 如果在服务器上插入的东西,它是不立即在客户端上可用的,在的情况下,它是发布到客户端

  • Meteor doesn't publish all data to the client unless autopublish is in to your project.
  • If you insert something on the server, it is not immediately available on the client, in the case that it is published to the client

这是您要查找的令牌的方式是你打电话后立即 Meteor.call ,它不太可能数据会一直在这一点上收到的客户端。

The way that you look for the tokens is right after you call Meteor.call, it's not very likely the data would have been received on the client at this point.

我不知道这是否会令任何区别,但你在服务器上使用异步JavaScript。流星使用纤维和你没有做到这一点。

I'm not sure whether this would make any difference, but you're using asynchronous javascript on the server. Meteor uses fibers and you don't have to do this.

这是在有帮助的,如果有问题的流星就会报错。你可以简单地做到这一点:

It's helpful in that, if there is a problem meteor will throw an error. You can simply do this:

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

要检查记号是否已经插入,最好的办法是使用流星蒙戈控制台,而你的应用程序正在运行,并检查记号是否在那里,这是实际蒙戈外壳到数据库中,以便流星的发布延迟/缺少的发布方法可能不会是一个问题。

To check whether the token has been inserted, the best way is to use the meteor mongo console while your app is running and check whether the tokens are in there, this is the actual mongo shell to the database so meteor's publish delay/lack of publish method is likely not to be an issue.

meteor mongo
>db.tokens.find();

(如果你的集合称为令牌,即: VAR PushTokens =新Mongo.Collection(标记)

这篇关于Meteor.methods失败,在服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆