【问题标题】:TypeError: response.hits[i].hit_set_id.toLowercase is not a functionTypeError: response.hits[i].hit_set_id.toLowercase 不是函数
【发布时间】:2021-12-10 09:25:24
【问题描述】:

每当我运行此代码时,我都会在控制台上收到该错误:

const response = JSON.parse(`{"version":1,"tests":[],"hits":[{"title":"Answer one of our market research surveys (~ 2 to 12 minutes + 1-2 minutes for demographics)","hit_set_id":"36R3RQSDQUGLBEZ545II11576K307C","requester_name":"UXreviewer.io ResearchLab","requester_id":"A1HXBVQANVSV6H","reward":"0.26","description":"Give us your opinion about usability, products, and marketing. The survey itself will be about ~ 2 to 12 minutes; in addition, 1-2 minutes for demographics are required beforehand.","duration_in_seconds":3600,"qualifications":{"project_requirements":[{"qualification_type_id":"00000000000000000071","comparator":"In","worker_action":"AcceptHit","qualification_values":["AL","AM","AN","AQ","AR","AT","AU","BE","BG","BY","CU","CZ","DE","DK","ES","ET","FR","GE","GF","IL","IT","JP","NL","NO","NP","PF","PL","PT","TF","US"],"caller_meets_requirement":true,"qualification_type":{"qualification_type_id":"00000000000000000071","name":"Location","visibility":true,"description":"The Location Qualification represents the location you specified with your mailing address.  Some HITs may only be available to residents of particular countries, states, provinces or cities.","has_test":false,"is_requestable":false,"keywords":"location"},"caller_qualification_value":{"integer_value":null,"locale_value":{"country":"US","subdivision":"IL"}}}]},"last_seen":1635091621,"requester_hourly":null}]}
`);

for (let i = 0; i < response.hits.length; i++) {
      console.log(response.hits[i]);
      console.log(response.hits[i].hit_set_id);
      const hitId = response.hits[i].hit_set_id.toLowercase();
      const title = response.hits[i].title.toLowercase();
      const requesterName = response.hits[i].requester_name.toLowercase();
      const requesterId = response.hits[i].requester_id.toLowercase();
}

从响应对象可以看出,response.hits[i].hit_set_id 是一个字符串,但是当我尝试对其调用 toLowerCase() 时,我得到了那个异常。老实说,我不知道发生了什么。

【问题讨论】:

    标签: javascript typeerror


    【解决方案1】:

    .toLowerCase() 区分大小写。

    【讨论】:

    • 就是这样,非常感谢,无论出于何种原因,VS code 都向我建议了这个,我只是想都没想就一直按 Tab。
    【解决方案2】:

    你在多个地方拼错了toLowerCase

    for (let i = 0; i < response.hits.length; i++) {
          console.log(response.hits[i]);
          console.log(response.hits[i].hit_set_id);
          const hitId = response.hits[i].hit_set_id.toLowerCase();
          const title = response.hits[i].title.toLowerCase();
          const requesterName = response.hits[i].requester_name.toLowerCase();
          const requesterId = response.hits[i].requester_id.toLowerCase();
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-13
      • 2020-12-23
      • 1970-01-01
      • 2020-06-29
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 2021-06-05
      相关资源
      最近更新 更多