【问题标题】:uncaught reference error, notification not defined未捕获的引用错误,未定义通知
【发布时间】:2019-02-10 12:06:41
【问题描述】:

我正在尝试向我的 rails 应用程序添加通知。基本上,我从 JSON 端点获取它们,然后尝试使用 CoffeescriptHere's the JSON array 获取它们并将它们附加到我的 html 中。问题出在 handleSuccess 方法中,我得到未捕获的引用错误,通知未定义

notifications.js.coffee

 class Notifications
  constructor: ->
   @notifications = $("[data-behavior='notifications']")
   @setup() if @notifications.length > 0

  setup: ->
   $.ajax(
      url: "/notifications.json"
      dataType: "JSON"
      method: "GET"
      success: @handleSuccess
    )

  handleSuccess: (data) =>
   console.log(data)
   items = $.map data, (notification) ->
      "<a class='dropdown-item' href='#{notification.url}'># 
      {notification.action} #{notification.notifiable.type}</a>"

jQuery ->
 new Notifications

我从 localhost:3000/notifications.json 得到什么

[{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/10#list_10"}, {"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/10#list_10"},{"actor" :"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/10#list_10"},{"actor":"emanuelcoen" ,"action":"liked","notifiable":{"type":"your list"},"url":"/lists/10#list_10"},{"actor":"emanuelcoen","action" :"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked" ,"notifiable":{"type":"your list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable" :{"type":"your list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type ":"你的名单"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"你的list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"}, “网址”:“/列表/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17 "},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17"}]

【问题讨论】:

  • 感谢您的帮助!问题是,我在服务器日志中找不到错误。在服务器日志中,我只看到在每个页面加载时它都会构建 JSON,但之后我看不到错误在哪里。你能告诉我在哪里可以找到它吗?另外:我在 AJAX 调用中指定了 dataType JSON,为什么它不是数组?
  • http://yourserver/notifications.json 返回什么?
  • 更新问题

标签: javascript ruby-on-rails coffeescript


【解决方案1】:

您传递给$.map 的函数中存在缩进错误。它下面的字符串必须缩进,否则它假定您将一个空函数传递给映射,并且它后面的行会引发错误,因为 notification 未定义。

  handleSuccess: (data) =>
   console.log(data)
   items = $.map data, (notification) ->
     "<a class='dropdown-item' href=''>#{notification.actor}</a>"

更新

关于通知未显示在页面上的评论 - 您没有调用任何代码来将生成的 html 字符串添加到 DOM。您可以为此使用$.append

  handleSuccess: (data) =>
   console.log(data)
   for notification in data
     @notifications.append(
       "<a class='dropdown-item' href=''>#{notification.actor}</a>")

没有必要在通知数组上使用$.map,因为我们只是在另一个循环中渲染它们,所以我用一个 Coffeescript 理解替换了它。

【讨论】:

  • 嗨,感谢您的帮助 :) 缩进起作用了,我在控制台中不再出现任何错误,并且我在控制台中看到了数组,但它仍然没有像应有的那样附加 html。任何想法?我编辑了答案以显示更新后的咖啡脚本文件和控制台的屏幕截图。
  • 嗨!它不适用于此代码,我收到 SyntaxError: [stdin]:18:1: unexpected indentation。当我更改标识时,应用程序可以运行,但没有附加任何内容。
  • @EmanuelCoen 抱歉,我遗漏了一组括号。我更新了代码
  • 上升。我以为我已经包含了链接,对不起:D gorails.com/episodes/in-app-navbar-notifications
  • 非常感谢您的帮助!完美运行:)
猜你喜欢
  • 2013-10-27
  • 2011-09-25
  • 2021-11-01
  • 2017-11-22
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多