我正在尝试在字符串中的#后测试空值。我已经尝试过各种各样的方法,但在提交测试数据时,我总是得到一个无法读取属性'1'的null。我已经找出了我能想到的错误,但这个我似乎无法解决。请记住,我是一个初学者,我没有编程,因为cobol天和我最后一次在javascript工作是在21世纪初。
//启动测试数据,可能通过的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语句中。我知道它的凌乱,但说实话,我很惊讶我到目前为止。
答案 0 :(得分:0)
看。如果您的测试字符串与您的正则表达式不匹配,则pstr
指定为null。除了在下一个if
条件中,您还尝试检查pstr
的第一个元素,而不检查空值:
if (pstr[1] !== null && pstr[2] !== null){ //doesnt work either =(
因此,我认为您需要在第二个pstr!==null
中添加if
,或者从此if
内移动所有条件分支,然后再移动前一个if
语句的一部分。
答案 1 :(得分:0)
var pstr = test.match(/#(\d{1,3})([a-zA-Z])/);
表示如果找不到正则表达式的匹配项,则pstr
为null
在这种情况下,pstr
的任何索引(如pstr [1],pstr [2])都会抛出你所描述的错误:
无法阅读财产&#39; n&#39;为null
<强>解决方案:强>
在使用索引之前,请检查变量是否具有值
if(pstr !== null) {
// do something with pstr[1]
}
修改强>
正如nnnnnn正确指出的那样,你不能在字符串中明确地存储一个空值。