问题描述
我正在尝试调试JS mini项目。其中一些显示:
未捕获的TypeError:无法读取null的属性“长度” 在任务(xx)
此任务是关于:检查给定的字符串是否包含相等数量的p和t。
我遵循了该错误的消息,据我了解,如果变量的长度为null或0,这意味着它是未定义的且无法验证。因此,我尝试使用以下代码,但未成功:
if (p < 0 || t< 0)
or
if (task55word == null)
task55 = () => {
const task55word = document.getElementById("task55").value;
const task55ans = document.getElementById("task55ans")
const wordP = task55word.toLowerCase().match(/p/g);
const wordT = task55word.toLowerCase().match(/t/g);
const p = wordP.length;
const t = wordT.length;
if (p == t) {
task55ans.textContent = 'equal';
} else {
task55ans.textContent = 'not equal';
}
};
我做错了什么?
解决方法
尝试此代码。如果没有匹配项,则client.on('message',(topic,payload,packet) => {
(async () => {
await msgMQTT.handleMQTT_messages(topic,process.env.STORAGE,MongoDBClient)
})
})
返回match
。
null