2017-08-24 21 views
0

這是我從終端獲取我的錯誤我收到服務器異常錯誤終端 - 流星

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' } 

這是流星方法我寫在服務器

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) " 

任何人,如果你有這個問題?

回答

0

你的代碼假定一個紀錄會被發現,但最好是在防守你的編碼,並假定事情都可能出錯,這是你的代碼:

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調用將返回一條記錄,但它返回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調用一些較爲抗跌的代碼,但我會讓你去做的。

此代碼現在不會炸燬(除非客戶未找到)

+0

我加入上面的代碼,但得到的ReferenceError:在調用方法的customerId沒有定義「異常‘getCustomerNameByAppIdReactive’{堆棧:「的ReferenceError:在[對象的對象] .getCustomerNameByAppIdReactive(服務器/功能沒有被定義的customerId \ N/searchFunctions.js:185:36)\ n「 – vishnusaran

+0

我在」return Customer.findOne({_ id:appt 。.customerId})名稱;」之後,沒有錯誤感謝兄弟的幫助 – vishnusaran

1

的使用,如果條件檢查是否有返回值。如果你返回值爲null或未定義你會得到異常

let customerId = Appointments.findOne({_id: appointmentId}).customerId; 
if(customerId){ 

} 
else{ 

}