Skip to content Skip to sidebar Skip to footer

Uncaught Reference Error, Notification Not Defined

I am trying to add notifications to my rails app. Basically I get them from a JSON endpoint and I try to fetch them and append them to my html using some CoffeescriptHere's the JSO

Solution 1:

You have an indentation error in the function you are passing to your $.map. The string below it has to be indented, otherwise it assumes you are passing an empty function to map, and the line after it raises an error as notification isn't defined.

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

Update

Regarding your comment that the notifications aren't showing on the page - you aren't calling any code to add the html string you are generating to the DOM. You could use $.append for this.

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

There is no need to use $.map over the notifications array as we are just rendering them in another loop, so I replaced it with a single Coffeescript comprehension.

Post a Comment for "Uncaught Reference Error, Notification Not Defined"