vishnusaran
vishnusaran

Reputation: 139

I'm getting server Exception Error in terminal - 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 ??

Upvotes: 0

Views: 538

Answers (2)

Thusila Bandara
Thusila Bandara

Reputation: 293

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{ 

}

Upvotes: 1

Mikkel
Mikkel

Reputation: 7777

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)

Upvotes: 0

Related Questions