Skip to content Skip to sidebar Skip to footer

Typeerror: Cannot Read Property 'subject' Of Null

I am retrieving data of Template collection from mongodb. so my problem is when i give a wrong templateName the program is supposed to catch a error. But it doesn't do so. program

Solution 1:

Mongodb-native (the client library you're using) won't raise an error if your find did not return any document. Errors are reserved for connectivity or syntax problems.

Therefore you must test the variable existence before using it, something like:

Template.findOne({ name: templateName }, function (err, template) {
    if (err === null && template == null) {
      // no error, but no result found
      err = newError(templateName + ' not found');
    }

    if (err) {
      console.log('Error occured');
      console.log(err.message);
      // early return to avoid another indentation :) returncallback(err);
    }
    template_subject = template.subject;
    template_html = template.dataMsg;

Solution 2:

You can check before line no:8. As subject is showing null.

if(template.subject && template.dataMsg){
    // ok
} else {
    // wrong templateName
}

Post a Comment for "Typeerror: Cannot Read Property 'subject' Of Null"