为什么先定义警报,然后再两行,然后不是(流星)? [英] Why is alert de[fine]d, and then two lines later, it's not (Meteor)?

查看:93
本文介绍了为什么先定义警报,然后再两行,然后不是(流星)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码将文档插入到MongoDB集合中:

Meteor.methods({
    'insertPerson': function(firstname, lastname, streetaddr1, streetaddr2, placename, stateorprov, zipcode, emailaddr, phone, notes) {
        console.log('insertPerson reached'); // TODO: Remove before deploying
        check(firstname, String);
        . . .
        alert('phone is ' + phone);
        var textAddrAsEmailAddr = Meteor.call('getTextAddrAsEmailAddr', phone); 
        alert('textAddrAsEmailAddr is ' + textAddrAsEmailAddr);
        . . .

...我看到第一个警报(电话是087163281"),但是没有看到第二个警报;流星控制台显示警报未定义:

I20151022-07:22:49.659(-7)? insertPerson reached
I20151022-07:22:49.881(-7)? Exception while invoking method 'insertPerson' Refer
enceError: alert is not defined
I20151022-07:22:49.881(-7)?     at [object Object].Meteor.methods.insertPerson (
both/methods.js:15:9)
I20151022-07:22:49.882(-7)?     at maybeAuditArgumentChecks (livedata_server.js:
1692:12)
I20151022-07:22:49.882(-7)?     at livedata_server.js:708:19
I20151022-07:22:49.882(-7)?     at [object Object]._.extend.withValue (packages/
meteor/dynamics_nodejs.js:56:1)
I20151022-07:22:49.883(-7)?     at livedata_server.js:706:40
I20151022-07:22:49.883(-7)?     at [object Object]._.extend.withValue (packages/
meteor/dynamics_nodejs.js:56:1)
I20151022-07:22:49.883(-7)?     at livedata_server.js:704:46
I20151022-07:22:49.884(-7)?     at tryCallTwo (C:\Users\Clay\AppData\Local\.mete
or\packages\promise\0.5.0\npm\node_modules\meteor-promise\node_modules\promise\l
ib\core.js:45:5)
I20151022-07:22:49.884(-7)?     at doResolve (C:\Users\Clay\AppData\Local\.meteo
r\packages\promise\0.5.0\npm\node_modules\meteor-promise\node_modules\promise\li
b\core.js:171:13)
I20151022-07:22:49.885(-7)?     at new Promise (C:\Users\Clay\AppData\Local\.met
eor\packages\promise\0.5.0\npm\node_modules\meteor-promise\node_modules\promise\
lib\core.js:65:3)

运行时引擎似乎有短期内存丢失的坏情况-为什么它首先识别出alert(),然后在几毫秒后却不识别出它?

这里是被调用的方法(仅次于上面的一种):

,
    'getTextAddrAsEmailAddr': function(phone) {
        this.unblock();
        var restcall = 'http://www.gettextemail.com/number.lookup.php?number=' + phone;
        return Meteor.http.call("GET", restcall);
    }

上面的最后一个代码块可能存在问题,但是即使那是问题,为什么它会抱怨alert()未定义?难道那已经变了气,遭受了虚拟的肺叶切除术?

更新

为了确保alert()不会引起问题,我将其更改为console.log()s,现在它认为通话"未定义:

I20151022-07:59:07.240(-7)? insertPerson reached
I20151022-07:59:07.365(-7)? phone is 0871632810
I20151022-07:59:07.365(-7)? Exception while invoking method 'insertPerson' TypeE
rror: Cannot call method 'call' of undefined
I20151022-07:59:07.365(-7)?     at [object Object].Meteor.methods.getTextAddrAsE
mailAddr (both/methods.js:37:28)

methods.js中的第37行是:

return Meteor.http.call("GET", restcall);

这有什么问题吗,还是... ???

解决方案

这是Meteor的一种有趣行为,如果您在客户端和服务器端都定义了Meteor方法,则可以在浏览器上获得警报,并且服务器中的错误日志.

您可以将Meteor方法仅用于服务器端,方法是将其放在/server文件夹中,并仅使用console.log.

或者,您也可以将代码包装在以下位置:

if (Meteor.isClient) {
    // code
}

并通过警报保持快乐状态.

更新:

在其中定义Meteor方法的地方是一个判断调用.将其同时留在客户端/服务器上的一个巨大优势是启用了Meteor的延迟补偿.

请参见乐观的用户界面部分: https://www.meteor.com/tutorials/blaze/security-with-methods

与此同时,这意味着您必须注意客户端和服务器都将调用您的方法这一事实.您的代码必须同时处理两种情况,这意味着在一个环境中定义但不在另一环境中定义的函数(例如alert)将需要特别注意.

I've got this code to insert a Document into a MongoDB Collection:

Meteor.methods({
    'insertPerson': function(firstname, lastname, streetaddr1, streetaddr2, placename, stateorprov, zipcode, emailaddr, phone, notes) {
        console.log('insertPerson reached'); // TODO: Remove before deploying
        check(firstname, String);
        . . .
        alert('phone is ' + phone);
        var textAddrAsEmailAddr = Meteor.call('getTextAddrAsEmailAddr', phone); 
        alert('textAddrAsEmailAddr is ' + textAddrAsEmailAddr);
        . . .

...I see the first alert ("phone is 087163281"), but not the second; the Meteor console says alert is not defined:

I20151022-07:22:49.659(-7)? insertPerson reached
I20151022-07:22:49.881(-7)? Exception while invoking method 'insertPerson' Refer
enceError: alert is not defined
I20151022-07:22:49.881(-7)?     at [object Object].Meteor.methods.insertPerson (
both/methods.js:15:9)
I20151022-07:22:49.882(-7)?     at maybeAuditArgumentChecks (livedata_server.js:
1692:12)
I20151022-07:22:49.882(-7)?     at livedata_server.js:708:19
I20151022-07:22:49.882(-7)?     at [object Object]._.extend.withValue (packages/
meteor/dynamics_nodejs.js:56:1)
I20151022-07:22:49.883(-7)?     at livedata_server.js:706:40
I20151022-07:22:49.883(-7)?     at [object Object]._.extend.withValue (packages/
meteor/dynamics_nodejs.js:56:1)
I20151022-07:22:49.883(-7)?     at livedata_server.js:704:46
I20151022-07:22:49.884(-7)?     at tryCallTwo (C:\Users\Clay\AppData\Local\.mete
or\packages\promise\0.5.0\npm\node_modules\meteor-promise\node_modules\promise\l
ib\core.js:45:5)
I20151022-07:22:49.884(-7)?     at doResolve (C:\Users\Clay\AppData\Local\.meteo
r\packages\promise\0.5.0\npm\node_modules\meteor-promise\node_modules\promise\li
b\core.js:171:13)
I20151022-07:22:49.885(-7)?     at new Promise (C:\Users\Clay\AppData\Local\.met
eor\packages\promise\0.5.0\npm\node_modules\meteor-promise\node_modules\promise\
lib\core.js:65:3)

It seems as if the runtime engine has a bad case of short-term memory loss - why does it recognize alert() at first and then, milliseconds later, not recognize it?

Here is the method (just below the one above) that is being called:

,
    'getTextAddrAsEmailAddr': function(phone) {
        this.unblock();
        var restcall = 'http://www.gettextemail.com/number.lookup.php?number=' + phone;
        return Meteor.http.call("GET", restcall);
    }

It may be that there's a problem with the last code block above, but even if that's the problem, why would it complain about alert() being undefined? Has it been that transmogrified, that it has suffered a virtual lobotomy?

UPDATE

Just to be sure that the alert()s weren't causing a problem, I changed them to console.log()s, and now it thinks "call" is undefined:

I20151022-07:59:07.240(-7)? insertPerson reached
I20151022-07:59:07.365(-7)? phone is 0871632810
I20151022-07:59:07.365(-7)? Exception while invoking method 'insertPerson' TypeE
rror: Cannot call method 'call' of undefined
I20151022-07:59:07.365(-7)?     at [object Object].Meteor.methods.getTextAddrAsE
mailAddr (both/methods.js:37:28)

Line 37 in methods.js is:

return Meteor.http.call("GET", restcall);

Is there something wrong with that, or...???

解决方案

This is a fun behavior of Meteor where if you define your Meteor method on both client and server-side, you can get both the alert on your browser AND an error log in the server.

You can define the Meteor method purely for servers-side by putting it in the /server folder and use console.log exclusively.

Alternatively you can wrap your code in:

if (Meteor.isClient) {
    // code
}

and go on your merry way with alerts.

Update:

Where you define your Meteor methods is a judgement call. One huge advantages to leaving it on both client/server is enabling Meteor's latency compensation.

See the Optimistic UI section: https://www.meteor.com/tutorials/blaze/security-with-methods

At the same time doing so means you will have to beware of the fact that both the client AND the server will call your method. Your code will have to handle both cases, meaning functions that are defined on one environment but not the other (such as alert) will need special attention.

这篇关于为什么先定义警报,然后再两行,然后不是(流星)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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