无法读取null javascript属性'length'检查给定的字符串是否包含字母:p&t

问题描述

我正在尝试调试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