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

标签 javascript jquery sql

    $.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/

相关文章:

javascript - 检查输入的格式和值

c# - 如何使用 Join 搜索字段?

mysql - MySQL JOIN 的评估顺序是什么?

javascript - 始终需要加载插件

javascript - 从 DOM 中删除元素时,不会调用 $destroy

javascript - jQuery inArray 和 Javascript IndexOf 返回部分匹配。我需要检查完整匹配

javascript - 自动调整页面上多个文本区域的大小

javascript - 安装 gulp 和 pdf.js 时出现问题

javascript - 将 JSON 字符串发送到 cherrypy

MySQL使用 'where not exists'插入多行并且没有主键