我有以下代码将文档插入 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”),但没有看到第二个; Meteor 控制台显示警报未定义:
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(),现在它认为“call”未定义:
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 的延迟补偿。
请参阅乐观 UI 部分:https://www.meteor.com/tutorials/blaze/security-with-methods
同时这样做意味着您必须注意客户端和服务器都会调用您的方法。您的代码必须处理这两种情况,这意味着在一种环境中定义但不在另一种环境中定义的函数(例如 alert
)需要特别注意。
关于javascript - 为什么Alert De [Fine] D,然后两行之后,不是( meteor )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33283706/