我在终端中收到服务器异常错误 - Meteor
I'm getting server Exception Error in terminal - Meteor
这是我从终端得到的错误
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' }
这是我在服务器写的meteor方法
getCustomerNameByAppIdReactive: function(appointmentId){
let customerId = Appointments.findOne({
_id: appointmentId}).customerId;
if (customerId == “1”){
return “Walk-In Customer”;
} else {
return Customers.findOne({_id:customerId}).name;
}
},
这是来自客户端的反应式方法调用
getCustomerName: (appointmentId)=>{
return ReactiveMethod.call(“getCustomerNameByAppIdReactive”,appointmentId);
},
此方法运行良好,但在终端中出现错误
"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) "
任何与此问题相关的人??
您的代码假设会找到一条记录,但最好在您的编码中采取防御措施,并假设事情可能会出错,这是您的代码:
getCustomerNameByAppIdReactive: function(appointmentId){
let customerId = Appointments.findOne({
_id: appointmentId}).customerId;
if (customerId == “1”){
return “Walk-In Customer”;
} else {
return Customers.findOne({_id:customerId}).name;
}
},
对 Appointments.findOne 的调用将 return 一条记录,但它正在 returning null/undefined。因此错误消息:
Cannot read property \'customerId\' of undefined
如果您将代码重组为防御性的,它会产生更好的结果,例如
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;
}
},
您可以在 Customers.findOne 调用中编写一些更具防御性的代码,但我会将其留给您。
这段代码不会爆炸(除非找不到客户)
使用 if 条件检查是否有 return value.if 你的 return 值为 null 或 undefined 你会得到异常
let customerId = Appointments.findOne({_id: appointmentId}).customerId;
if(customerId){
}
else{
}
这是我从终端得到的错误
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' }
这是我在服务器写的meteor方法
getCustomerNameByAppIdReactive: function(appointmentId){
let customerId = Appointments.findOne({
_id: appointmentId}).customerId;
if (customerId == “1”){
return “Walk-In Customer”;
} else {
return Customers.findOne({_id:customerId}).name;
}
},
这是来自客户端的反应式方法调用
getCustomerName: (appointmentId)=>{
return ReactiveMethod.call(“getCustomerNameByAppIdReactive”,appointmentId);
},
此方法运行良好,但在终端中出现错误
"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) "
任何与此问题相关的人??
您的代码假设会找到一条记录,但最好在您的编码中采取防御措施,并假设事情可能会出错,这是您的代码:
getCustomerNameByAppIdReactive: function(appointmentId){
let customerId = Appointments.findOne({
_id: appointmentId}).customerId;
if (customerId == “1”){
return “Walk-In Customer”;
} else {
return Customers.findOne({_id:customerId}).name;
}
},
对 Appointments.findOne 的调用将 return 一条记录,但它正在 returning null/undefined。因此错误消息:
Cannot read property \'customerId\' of undefined
如果您将代码重组为防御性的,它会产生更好的结果,例如
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;
}
},
您可以在 Customers.findOne 调用中编写一些更具防御性的代码,但我会将其留给您。
这段代码不会爆炸(除非找不到客户)
使用 if 条件检查是否有 return value.if 你的 return 值为 null 或 undefined 你会得到异常
let customerId = Appointments.findOne({_id: appointmentId}).customerId;
if(customerId){
}
else{
}