gpt4 book ai didi

javascript 未捕获类型错误 : Cannot read property 'date_notify' of undefined

转载 作者:行者123 更新时间:2023-12-02 19:40:10 24 4
gpt4 key购买 nike

    $.ajax({
url: "notifications.php",
dataType: "json",
success: function(responseJSON) {
if (responseJSON.length > 0) {
document.title = document.title.replace(/^(?:\(\d+\))?/, "(" + responseJSON.length + ") ")
for (var i=0; i<10; i++) {
console.log(responseJSON[i].date_notify')
}
}
}, error : function(x) {
console.log(x.responseText)
}
})

在 Chrome 中我遇到此错误:

Uncaught TypeError: Cannot read property 'date_notify' of undefined

我在这部分发现了for (var i=0; i<10; i++)应替换为 for (var i=0; i<responseJSON.length; i++)问题是我只想得到 10 个结果...在我的 sql 部分我没有限制查询。这是我的查询

SELECT users.lastname, users.firstname, users.screenname, notifications.notify_id, 
notifications.tagged_by, notifications.user_id, notifications.post_id,
notifications.type, notifications.action, notifications.date_notify,
notifications.notify_id
FROM website.users users INNER JOIN website.notifications notifications
ON (users.user_id = notifications.user_id)
WHERE notifications.user_id = ? and notifications.action = ?
ORDER BY notifications.notify_id DESC
//LIMIT 10

有什么办法可以改变这个吗?

最佳答案

在循环中使用 ANDed 条件:

for(var i=0; i<responseJSON.length && i<10; i++)

这样,当 i 超过条目数或达到 10(以先发生者为准)时,循环就会中断。如果条目数小于10,则第一个条件先为假,如果记录数超过10,则第二个条件先为假。

关于javascript 未捕获类型错误 : Cannot read property 'date_notify' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10471361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com