简体   繁体   English

TypeError: Cannot read property 'title' of undefined Nodejs Mysql 查询结果

[英]TypeError: Cannot read property 'title' of undefined Nodejs Mysql Query result

This error doesn't happen always, but it's been happening frequently and i couldn't find any solution for it.这个错误并不总是发生,但它经常发生,我找不到任何解决方案。 Any help would be appreciated.任何帮助,将不胜感激。

PS: the table in the database has the 'title' column and there are data in there. PS:数据库中的表有'title'列,里面有数据。

The Code:编码:

 async function selectNotificationData(control_message_id){
const sql = await new Promise((res,rej)=>{
    let query = "SELECT title, body ,url_push, img_push, url_type, status,silent,channel FROM control_message WHERE id_control_message =?";
    con.query(query, [control_message_id],(err,row)=>{
        if(err) throw err;
        let n =row[0];
        let pf=null;
        try{
            pf =  setPerFlagOptmized(n.title,n.body);
            res({not_data:n,pf})
        }
        catch (err) {

            console.log("[Row : "+row[0]+"]");
            console.log("[n : "+n+"]");
            console.log("ERROR IN SELECTING NOT DATA OR SETTING THE FLAG : "+err);
        }


    })
}).catch((error)=>{
    console.log("ERROR IN SELECTING Notification DATA : "+error);
});

The Error:错误:

app_1    | /usr/src/app/node_modules/mysql/lib/protocol/Parser.js:437
app_1    |       throw err; // Rethrow non-MySQL errors
app_1    |       ^
app_1    |
app_1    | TypeError: Cannot read property 'title' of undefined
app_1    |     at Query.con.query (/usr/src/app/v4/message/controllers/expandWorker.js:669:57)
app_1    |     at Query.<anonymous> (/usr/src/app/node_modules/mysql/lib/Connection.js:526:10)
app_1    |     at Query._callback (/usr/src/app/node_modules/mysql/lib/Connection.js:488:16)
app_1    |     at Query.Sequence.end 
(/usr/src/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
app_1    |     at Query._handleFinalResultPacket 
(/usr/src/app/node_modules/mysql/lib/protocol/sequences/Query.js:149:8)
app_1    |     at Query.EofPacket 
(/usr/src/app/node_modules/mysql/lib/protocol/sequences/Query.js:133:8)
app_1    |     at Protocol._parsePacket 
(/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:291:23)
app_1    |     at Parser._parsePacket 
(/usr/src/app/node_modules/mysql/lib/protocol/Parser.js:433:10)
app_1    |     at Parser.write (/usr/src/app/node_modules/mysql/lib/protocol/Parser.js:43:10)
app_1    |     at Protocol.write (/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:38:16)

I tried to log我试图登录

[Row : undefined]
[n : undefined]

The setPerFlagOptmized function: setPerFlagOptmized function:

 function setPerFlagOptmized(title,body){
    var t = (title.includes("|*") || title.includes("*|") || title.includes("{{") || title.includes("}}"));
    var b = (body.includes("|*") || body.includes("*|") || body.includes("{{") || body.includes("}}"));
    return (t || b) ? 1:0;
}

This probably happens when no record is found for the given query.当没有找到给定查询的记录时,可能会发生这种情况。 err in the callback will not be defined in this case, instead row is just an empty array.在这种情况下,不会定义回调中的err ,而row只是一个空数组。

Since you don't have a check for that and directly access row[0] , which yields undefined , the error is thrown for n.title .由于您没有对此进行检查并直接访问产生undefinedrow[0] ,因此会为n.title引发错误。 So you should add the following check:因此,您应该添加以下检查:

if(err) throw err;

if (!row.length) {
   // handle error - throw or return here
}
// ..rest of the code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM