I'm getting server Exception Error in terminal - Meteor - meteor

This is the error i’m getting from terminal
Exception while invoking method 'getCustomerNameByAppIdReactive' { stack: 'TypeError: Cannot read property \'customerId\' of undefined
at [object Object].getCustomerNameByAppIdReactive (server/Functions/searchFunctions.js:167:20)
at [object Object].methodMap.(anonymous function) (packages/meteorhacks_kadira/lib/hijack/wrap_session.js:164:1)
at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1711:12)
at packages/ddp-server/livedata_server.js:711:19
at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
at packages/ddp-server/livedata_server.js:709:40
at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
at packages/ddp-server/livedata_server.js:707:46
at tryCallTwo (/home/saran/.meteor/packages/promise/.0.7.3.3xbt0b++os+web.browser+web.cordova/npm/node_modules/promise/lib/core.js:45:5)
at doResolve (/home/saran/.meteor/packages/promise/.0.7.3.3xbt0b++os+web.browser+web.cordova/npm/node_modules/promise/lib/core.js:200:13)', I20170824-11:47:30.445(5.5)? source: 'method' }
this is the meteor method i wrote it in the server
getCustomerNameByAppIdReactive: function(appointmentId){
let customerId = Appointments.findOne({
_id: appointmentId}).customerId;
if (customerId == “1”){
return “Walk-In Customer”;
} else {
return Customers.findOne({_id:customerId}).name;
}
},
this is reactive-method call from client
getCustomerName: (appointmentId)=>{
return ReactiveMethod.call(“getCustomerNameByAppIdReactive”,appointmentId);
},
this method working finely but getting the error in terminal as
"Exception while invoking method ‘getCustomerNameByAppIdReactive’ { stack: 'TypeError: Cannot read property ‘customerId’ of undefined\n at [object Object].getCustomerNameByAppIdReactive (server/Functions/searchFunctions.js:167:20)\n at [object Object].methodMap.(anonymous function) "
Anyone if you had related with this issue ??

use if condition to check whether there is a return value.if your return value is null or undefined you will get the exception
let customerId = Appointments.findOne({_id: appointmentId}).customerId;
if(customerId){
}
else{
}

Your code assumes that a record will be found, but it's better to be defensive in your coding, and assume that things can go wrong, This is your code:
getCustomerNameByAppIdReactive: function(appointmentId){
let customerId = Appointments.findOne({
_id: appointmentId}).customerId;
if (customerId == “1”){
return “Walk-In Customer”;
} else {
return Customers.findOne({_id:customerId}).name;
}
},
The call to Appointments.findOne will return a record, but it's returning null/undefined. Hence the error message:
Cannot read property \'customerId\' of undefined
If you restructure your code to be defensive, it will yield better results, eg
getCustomerNameByAppIdReactive: function(appointmentId){
let appt = Appointments.findOne({
_id: appointmentId});
if (!appt) {
console.log("Can't find an appointment record for "+appointmentId)
return "Not found"
}
if (appt.customerId === “1”){
return “Walk-In Customer”;
} else {
return Customers.findOne({_id:customerId}).name;
}
},
You could write some more defensive code on the Customers.findOne call, but I'll leave that for you to do.
This code won't blow up now (unless the customer is not found)

Related

how to use Collection.find() inside route action of Meteor js

I have a peice of code;
Router.configure({
layoutTemplate: 'master_layout'
});
Router.map(function(){
this.route('generatePDF', {
path: '/pdf',
where: 'server',
action: function() {
console.log(voters.findOne().fetch());
var voters = voters.find();
....
}
});
How can I use any given collection inside action of routes.
i get error as below;
W20160510-20:19:29.909(5.5)? (STDERR) TypeError: Cannot call method 'findOne' of undefined
W20160510-20:19:29.910(5.5)? (STDERR) at [object Object].action (lib/routes.js:40:29)
W20160510-20:19:29.910(5.5)? (STDERR) at boundNext (packages/iron_middleware-stack/lib/middleware_stack.js:251:1)
Try this:
action: function () {
this.render('someTemplate',{
data: function(){
return Voters.find()
}
});
}
You've got multiple errors in your code.
Firstly, voters is undefined in the console.log line (as your error log says).
Secondly, Voters.findOne() returns a single document (assuming you have a collection called Voters). You can't call fetch on a document.
You can call fetch on a cursor. For example, Voters.find().fetch() would be fine.
Hope that helps

Undefined exception when running meteor subscribe from angular

My angular controller looks like this:
angular.module("campos").controller("HomeCtrl", ['$scope', '$meteor', '$rootScope', '$state', '$modal',
function ($scope, $meteor, $rootScope, $state, $modal) {
// $scope.users = $meteor.collection(Meteor.users, false).subscribe('users');
$meteor.autorun($scope, function () {
$scope.$meteorSubscribe('myOrgnization', 'crown').then(function () {
$scope.organization.forEach(function (org) {
console.log(org._id);
});
},
function (error) {
console.log(error);
});
});
$scope.organization = $meteor.collection(function(){
return Organizations.find({});
});
}
]);
The server code looks like :
Meteor.publish('myOrgnization', function(org){
return Organizations.find({
'code' : org
});
});
The client controller finished properly, from the console I can get the the record id properly. However, I got an exception thrown out:
TypeError: undefined is not a function
at http://localhost:8026/packages/urigo_angular.js?de756130fe1ce5aaa41d3967997c1b2090bcdd1b:390:12
at Array.forEach (native)
at Function._.each._.forEach (http://localhost:8026/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:149:11)
at diffArray (http://localhost:8026/packages/urigo_angular.js?de756130fe1ce5aaa41d3967997c1b2090bcdd1b:388:5)
at updateCollection (http://localhost:8026/packages/urigo_angular.js?de756130fe1ce5aaa41d3967997c1b2090bcdd1b:1310:3)
at http://localhost:8026/packages/urigo_angular.js?de756130fe1ce5aaa41d3967997c1b2090bcdd1b:1154:11
at http://localhost:8026/packages/angular_angular.js?feeaf4750c9fd3ceafa9f712f99a21ee6fef9b41:17819:31
at completeOutstandingRequest (http://localhost:8026/packages/angular_angular.js?feeaf4750c9fd3ceafa9f712f99a21ee6fef9b41:5529:10)
at http://localhost:8026/packages/angular_angular.js?feeaf4750c9fd3ceafa9f712f99a21ee6fef9b41:5806:7
home.ng.js:8 dPmZoCeJ9YbKxKA4u
It seems coming from angular defer, but I think I am following the subscribe syntax properly ? even I change it to use $meter.subscribe, it throws the same exception.
Any help will be much appreciated.
Thanks in advance.
You should assign the $scope collection after the promise returns:
$scope.$meteorSubscribe('myOrgnization', 'crown').then(function () {
$scope.organization = $scope.$meteorCollection(function(){
return Organizations.find({});
});
});
I would also suggest passing an object for your publish arguments:
$scope.$meteorSubscribe('myOrginization', {arg1: 'crown'}).then(...)
Then of course on the server you would get:
Meteor.publish('myOrgnization', function(org){
return Organizations.find({
'code' : org.arg1
});
});

Meteor client function not passing data to server function for Accounts.createUser

I've a client function for registering a user that passes a data object to the server to create a user. I see the data object fully on the client side, yet when the server function runs (registerNewUser) it fails with an error saying the data is undefined.
On the client, register.js:
if (Meteor.isClient){
Template.registrationStep2.events({
'submit form': function(e, template) {
e.preventDefault();
if (Session.get("registrationInstitutionID") && Session.get("registrationInstitutionRole")) {
var data = [];
var institutionID = template.find('#institutionID').value;
var institutionRole = template.find('#institutionRole').value;
var institutionName = template.find('#institutionName').value;
data.login = template.find('#login').value;
console.log("login: " + data.login);
var firstName = template.find('#firstName').value;
console.log("firstName: " + firstName);
var lastName = template.find('#lastName').value;
console.log("lastName: " + lastName);
data.email = template.find('#email').value;
console.log("email: " + data.email);
data.password = template.find('#password').value;
data.profile = {
firstName: firstName,
lastName: lastName,
role: institutionRole,
institutionID: institutionID
}
Meteor.call('registerNewUser', data, function(error, result){
Meteor.log.trace("User Data Object", data);
console.log(data);
if(error){
console.log("error", error);
Meteor.log.error("Error Creating User", error);
}
if(result){
console.log("SUCCESS adding user.");
Meteor.log.error("Success Result", result);
Session.set("flashType", "success");
Session.set("flashMessage", "Your account has been created.");
Router.go('registrationComplete');
}
});
} else {
// alert("Need institution");
$('#needInstitutionModal').modal('show');
}
}
});
}
Here is the server.js:
Meteor.methods({
'registerNewUser': function(data) {
Meteor.log.debug("User Data Object on Server", data);
console.log(data);
return Accounts.createUser({
username: data.login,
email: data.email,
password: data.password,
profile: data.profile
}
});
If I hardcode values for the createUser function all works OK. But when I use the data object I get errors like this for each field. (And I know from a console log the data object existed in the client before calling the server function).
Here is the specific error line in case it's hard to see below: Exception while invoking method 'registerNewUser' Error: Match error: Expected string, got undefined in field username
Here's the relevant output from the log:
I20150710-07:56:51.072(-5)? (07:56:51) [DEBUG # programs/server/packages/ostrio_loggerconsole.js:67] - Object:
I20150710-07:56:51.072(-5)? {
I20150710-07:56:51.072(-5)? "time": "2015-07-10T12:56:51.070Z",
I20150710-07:56:51.072(-5)? "level": "DEBUG",
I20150710-07:56:51.072(-5)? "message": "User Data Object on Server",
I20150710-07:56:51.072(-5)? "additional": []
I20150710-07:56:51.072(-5)? }
I20150710-07:56:51.075(-5)? logging data object on server ================+
I20150710-07:56:51.075(-5)? []
I20150710-07:56:51.077(-5)? Exception while invoking method 'registerNewUser' Error: Match error: Expected string, got undefined in field username
I20150710-07:56:51.078(-5)? at checkSubtree (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/check.js:177:13)
I20150710-07:56:51.078(-5)? at /Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/check.js:314:9
I20150710-07:56:51.078(-5)? at Function._.each._.forEach (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/underscore.js:147:22)
I20150710-07:56:51.078(-5)? at checkSubtree (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/check.js:308:5)
I20150710-07:56:51.078(-5)? at check (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/check.js:50:5)
I20150710-07:56:51.078(-5)? at createUser (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/accounts-password.js:795:3)
I20150710-07:56:51.078(-5)? at Object.Accounts.createUser (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/accounts-password.js:875:10)
I20150710-07:56:51.078(-5)? at [object Object].Meteor.methods.registerNewUser (app/server/server.js:17:25)
I20150710-07:56:51.078(-5)? at [object Object].methodMap.(anonymous function) (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/meteorhacks_kadira.js:2536:30)
I20150710-07:56:51.078(-5)? at maybeAuditArgumentChecks (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/ddp.js:2445:12)
I20150710-07:56:51.078(-5)? at /Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/ddp.js:1476:20
I20150710-07:56:51.078(-5)? at [object Object]._.extend.withValue (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/meteor.js:989:17)
I20150710-07:56:51.079(-5)? at /Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/ddp.js:1475:41
I20150710-07:56:51.079(-5)? at [object Object]._.extend.withValue (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/meteor.js:989:17)
I20150710-07:56:51.079(-5)? at [object Object]._.extend.protocol_handlers.method (/Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/ddp.js:1474:51)
I20150710-07:56:51.079(-5)? at /Users/srichardson/AppDevNew/ve-client-new/.meteor/local/build/programs/server/packages/meteorhacks_kadira.js:2425:38
I20150710-07:56:51.079(-5)? Sanitized and reported to the client as: Match failed [400]
Thanks much for the help.
I'm not sure why you declare data as an array when you use it as an object.
Try
var data = {};
instead.
I just tried making a barebones version of this myself and I get the same problem as you when I declare it as an array which is interesting.
On the client everything looks normal (It forgoes data being an array once you start using keys) but on the server as far as it is concerned data is an empty array. Weird result but declaring data as an object fixed it.

Meteor.publish() throw an exception in defer callback

I found this error in the server console:
Exception in defer callback: TypeError: undefined is not a function
at packages/ddp/livedata_server.js:1054:1
at Array.forEach (native)
at Function..each..forEach (packages/underscore/underscore.js:105:1)
at [object Object]._.extend._callStopCallbacks (packages/ddp/livedata_server.js:1053:1)
at [object Object]._.extend._deactivate (packages/ddp/livedata_server.js:1043:1)
at packages/ddp/livedata_server.js:803:1
at Function..each..forEach (packages/underscore/underscore.js:113:1)
at [object Object]._.extend._deactivateAllSubscriptions (packages/ddp/livedata_server.js:802:1)
at packages/ddp/livedata_server.js:444:1
at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
And here the code I use:
/* Source: http://phucnguyen.info/blog/how-to-publish-to-a-client-only-collection-in-meteor/ */
var subs = {};
Meteor.publish(
// THROW AN 'EXCEPTION IN DERFER CALLBACK: ...'
'helperPublication',
function () {
var subscription = this;
subs[subscription._session.id] = subscription;
Datasets.find().map(function (dataset) {
subscription.added(
'subdatasets',
dataset._id,
{
name: dataset.name,
data: [], // To avoid "Uncaught TypeError: Cannot read property 'length' of undefined" error on load
peaks: [] // Idem
}
)
});
subscription.onStop();
subscription.ready()
});
You can find the whole app in the following meteorpad: http://meteorpad.com/pad/6NDneym2qEW7pF9JM/ClearChrom
Ok, So with that info; I think the best way to go about doing this is to have separate collection for that data. Because then you can easily change how much data should be displayed. The publication for that collection could look like this:
Meteor.publish('data', function publishData (limit) {
return DataCollection.find({}, {
fields: {
name: 1,
data: 1,
peaks: 1
},
limit: limit
})
});
Note that the callback of the publication takes an argument limit. You can now subscribe to it like this:
Meteor.subscribe('data', 3)
And whenever you need more data you can just:
Meteor.subscribe('data', 6)
So this solution is reactive and very clean (in my opinion at least).
I also checked your existing script:
var subs = {};
Meteor.publish(
'helperPublication',
function (limit) {
var subscription = this;
subs[subscription._session.id] = subscription;
// I'd use forEach here, because you're not modifying the document
Datasets.find().forEach(function (doc) {
subscription.added(
'subdatasets',
doc._id,
{
name: doc.name,
// Whith the slice function you can get a subset of the data array
data: doc.data.slice(0, limit),
peaks: doc.peaks
}
)
});
// Calling `subscription.onStop` without an argument doesn't do anything
// Except maybe through an error
subscription.onStop(function () {
// clean the subscription from subs again
delete subs[subscription._session.id];
});
// This shouldn't be necessary.
// subscription.ready()
});
There are some issues with this still. For one thing I'd advise you to try avoiding meteor attributes with an underscore in front. They might be removed or changed in future releases.

Weird Error: No method 'find'?

I'm receiving a very weird error:
Exception from sub sda4Ho8DT6MXTKjvb TypeError: Object #<Object> has no method 'find'
I20140728-11:22:25.248(-4)? at null._handler (app/server/publications.js:4:18)
This is my code. Very simple pub/sub. No idea why I get this error.
publications.js
Meteor.publish('assets', function() {
//query = query || {};
//query.removed = false;
return Assets.find();
});
assets_col.js
Assets = new Meteor.Collection('assets');
router.js
waitOn: function() {
return Meteor.subscribe('assets');
}

Resources