我正在從mongodb中檢索Template collection的數據。所以我的問題是,當我給錯了templateName
程序應該會發生錯誤。但它並沒有這樣做。程序進一步&我得到錯誤TypeError: Cannot read property 'subject' of null
。TypeError:無法讀取屬性'subject'null
如何處理這件事?
Template.findOne({ name: templateName }, function (err, template) {
if (err) {
console.log('Error occured');
console.log(err.message);
callback(err);
}
else {
template_subject = template.subject;
template_html = template.dataMsg;
});
如果給出了錯誤的templateName,我想將錯誤返回給回調函數。
您可以在行號:8之前進行檢查 – Mahavir