2017-08-01 57 views
0

我想在字符串中的#后测试空值。我尝试过不同的方法,但是当提交测试数据时,我总是得到一个无法读取null的属性'1'。我已经发现了我能想到的错误,但这个我似乎无法解决。请记住,我是一个初学者,自从cobol日子以来,我还没有编程,而且我上一次工作javascript是在21世纪初。检查null只能得到“无法读取null属性'1'在nodejs

//启动试验数据,5个可能的字符串,可以通过

elt.message = '#1a' //goes through the script good 
elt.message = '#12b' // goes through 
elt.message = '#123c' //goes through 
elt.message = '' //is ignored 
elt.message = '# ' //crashes server 

//结束测试数据

//First lets test to see if # is in the message. If true then we will parse it and add it to the database. 
var str = elt.message; 
var substr = '#'; 
var vtest = str.indexOf(substr) > -1; 
if (vtest == 1){ 
var Vname = elt.author; 
console.log('We tested for # and the value is true'); 

//extracts the number and the letter after the # from incoming chat messages 
var test = elt.message; // replace with message text variable. 
var pstr = test.match(/#(\d{1,3})([a-zA-Z])/); 
if (pstr) { 
var numbers = pstr[1]; 
var character = pstr[2]; 
var chupp = character.toUpperCase(); //Converts the lowercase to uppercase 
} 

//Tests to see if neither the question number or the possible answer is left out 
//if (pstr[1] !== '' && pstr[2] !== ''){ //doesn't work =(
if (pstr[1] !== null && pstr[2] !== null){ //doesn't work either =(

console.log('we processed the numbers after the #sign and assigned the numbers and letter into variables.') 
console.log('The question number is: ' + pstr[1]); 
console.log('The letter processed is: ' + pstr[2]); 

// Grabs the date and converts it into the YYYYMMDD string. 
var dobj = new Date(); 
var dstr = dobj.toString(); 
var dsplit = dstr.split(' '); 
let currentdate = `${dobj.getMonth() < '9' ? `0${dobj.getMonth() + 1}` : 
    dobj.getMonth() + 1}`; 
    currentdate = `${dsplit[3]}${currentdate}${dsplit[2]}`; 
console.log(currentdate)//remove when done 

//checks to see what the highest question number is in the database 
var sel = con.query("SELECT * FROM questions WHERE ClassID = "+ currentdate + " ORDER BY QuesID DESC LIMIT 1", function (err, result){ 
    if (err) throw err; 
    console.log('Total number of question records: '+result[0].QuesID); 
    console.log('the script is querying with' + pstr[1]); 
    console.log('the scripts answer letter is ' + pstr[2]); 

    if (pstr[2] != '' && pstr[1] <= result[0].QuesID){ 
     var query = con.query("SELECT * FROM questions WHERE ClassID = " + currentdate + " AND QuesID = " + pstr[1], function (err, result) { // Selects the record based on the Date and the question number variables provided above 
     if (err) throw err; 
     console.log('it got past the test') 

    if (result[0].AnsweredFirst === '' && result[0].AnswerLetter === chupp) { //Test to see if the AnsweredFirst is empty and that the Answer letter matchs with whats on file 
    console.log('MATCH!');//remove when done 

    var sql = "UPDATE questions SET AnsweredFirst = '"+ Vname + "' WHERE ClassID = " + currentdate + " AND QuesID = " + pstr[1]; //Updates the record with the first person who answered the question in the AnsweredFirst field 
    con.query(sql, function (err, result) { 
     if (err) throw err; 
     console.log(Vname + " answered question " + pstr[1] + " First!"); 
    }); 
    } 
    }); 
} 
}); 
} else { 
    console.log('Either the question number or the letter was left blank so we are skipping'); //the viewer did not put in a proper number and letter after the # sign 
    } 
} else { 
    console.log('No signs of # so skipping queries') //if there is no # sign the message is not processed 
    }; 

我加入了脚本的其余部分以获得更好的主意。消息从聊天客户端传递到服务器。

我会尝试将代码块移到第一个if语句中。我知道它杂乱,但老实说,我很惊讶,我得到了这一点。

+1

如果你的正则表达式不匹配,那么'pstr'将是'null',所以'pstr [1]'会给出一个错误。在现有的if(pstr){...}块中移动这些测试。 *“字符串中的#后面的空值”* - 除非要查找子字符串“null”,否则字符串不能包含嵌入的空值。你可以请[编辑]你的问题,以显示相应的期望输出的一些示例输入? – nnnnnn

回答

0

var pstr = test.match(/#(\d{1,3})([a-zA-Z])/);

意味着,如果没有发现匹配为您正则表达式,然后pstrnull

在这种情况下

pstr任何索引(如PSTR [1],PST​​R [2])将引发错误您所描述:

无法读取空的财产 'N'

解决方案:

使用索引之前,检查变量的值或者不

if(pstr !== null) { 
    // do something with pstr[1] 
} 

编辑:

而作为nnnnnn正确地指出,你不能明确字符串中的空值。

+0

我试过如果使用 (pstr [1]!== null && pstr [2]!== null){但它不起作用。 – Palgrave

+0

这是错误的。当你的对象为null时,你不能读取它的属性(并将它与null进行比较)。如果(pstr!== null){ //使用pstr [1] } 这就像一个魅力一样工作,你必须检查对象本身为空 – mrid

+1

!没有更多的崩溃,非常感谢! – Palgrave

0

看。如果您的测试字符串与您的正则表达式不匹配,则将pstr分配给null。除了在接下来的if条件你试图检查pstr第一要素,而不检查它的空值:

if (pstr[1] !== null && pstr[2] !== null){ //doesnt work either =(

所以,我认为你需要无论是在第二if从这个if内,然后部分添加pstr!==null或移动所有条件分支之前的一个if声明。

相关问题