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:
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.