【问题标题】:Uncaught TypeError: Cannot read property in Chrome未捕获的类型错误:无法在 Chrome 中读取属性
【发布时间】:2014-03-24 12:33:21
【问题描述】:

我创建了一个脚本,它应该转到每个单独的链接,检查它的售价,如果它的售价等于或低于我输入的价格,它会在标题字符串上注明。 无论出于何种原因,在 Chrome 上执行它时,95% 的时间都会弹出以下错误: 未捕获的类型错误:无法读取 null 的属性“1”

JShint 也无济于事,因为无论我改变什么,它总是说同样的错误。如果有帮助,我认为错误在正则表达式行附近。

脚本:

var IDs = [136758649, 116770724, 136171998, 113325603, 111903483, 116786719, 136172656, 139152118, 121926643, 120759721] //Shortened down to a few IDs
var PriceWanting = Number(prompt("How much is the max you would do?"))
document.write('<p id = "title">Total number bought: 0 items for a total of 0</p>')
var buys = 0
var totalrobuxspent = 0
console.log("Bot started")
var loop = setInterval(function () {
    for (var i = 0; i < IDs.length; i++) {
        $.get(" http://m.roblox.com/items/" + IDs[i] + "/privatesales", function (data) {
            var Regex = /\<span class="currency-robux">([\d,]+)\<\/span\>/
            var PriceSelling = data.match(Regex)[1]
            PriceSelling = Number(PriceSelling.replace(",", ""))
            PriceSelling = PriceSelling * 1
            totalrobuxspent = totalrobuxspent + PriceSelling
            var remaining = PriceWanting - PriceSelling
            if (remaining >= -0.1) {
                buys = buys + 1
                document.getElementById("title").innerHTML = "Total number of items bought: " + buys + " for a total of " + totalrobuxspent
            }
        })
    }
}, 3636) //Long intervals so it doesn't lag too badly; mainly for me testing

【问题讨论】:

  • ;s 在哪里...(我知道它们不是必需的,但使用它们是一个好习惯)
  • String#match returns an Array or null if there were no matches.。在您从中读取 [1] 之前检查它是否返回 null。
  • DCoder 你应该给出一个完整的答案,因为我怀疑你是正确的:)
  • 第二个想法,我不知道如何检查它是否为空。谁能告诉我怎么做?在 LUA 中,如果 Regex == null 那么......但我知道这是非常不同的。
  • 您的 data.match(Regex)[1] 没有找到任何匹配项,因此它返回 null 并给您一个错误

标签: javascript regex


【解决方案1】:

您可以像这样检查它是否为空:

if (!x) {
    // x is not null; do something here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-22
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    相关资源
    最近更新 更多