Can't embed comments: "Cannot read propery 'forEach' of undefined"


(Esther Martz) #1

Hi,

I’m trying to embed posts as comments on my website but the only thing I’m getting is these errors: . Searching through these forums, I noticed this issue was first seen on version 1.4.0.beta1 (https://meta.discourse.org/t/how-can-i-troubleshoot-discourse-comments-embedding-feature/30323) , but I’m running on v1.6.0.beta8 +61 …

Maybe there’s something wrong with my script? (Jade)

script(type='text/javascript').
              DiscourseEmbed = { discourseUrl: 'https://forum.geekshubsacademy.com/',
              discourseEmbedUrl: 'https://geekshubsacademy.com/lms/introduccion-a-swift-beta/content/1/1/'};
              (function() {
              var d = document.createElement('script'); d.type = 'text/javascript'; d.async = true;
              d.src = DiscourseEmbed.discourseUrl + 'javascripts/embed.js';
              (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d);
              })();  

Thank you :slight_smile:


(Robin Ward) #2

I’m not sure familiar with Jade but that looks OK to me.

The #1 thing to check is that on the discourse side your embedded host is correct. It has to be the same as the host where you are embedding the posts. In your case, probably geekshubacademy.com.

If that doesn’t work, you can check the logs on your discourse by logging in with an admin account and going to /admin.


(Esther Martz) #3

Thank you for the fast reply!

As you said, this is the embedded host: .

And these are the logs shown @ /admin:

Uncaught TypeError: Cannot read property 'forEach' of undefined
Url: https://forum.geekshubsacademy.com/embed/comments?embed_url=https%3A%2F%2Fgeekshubsacademy.com%2Flms%2Fintroduccion-a-swift-beta%2Fcontent%2F1%2F1%2F
Line: 203
Column: 34
Window Location: https://forum.geekshubsacademy.com/embed/comments?embed_url=https%3A%2F%2Fgeekshubsacademy.com%2Flms%2Fintroduccion-a-swift-beta%2Fcontent%2F1%2F1%2F
Backtrace

TypeError: Cannot read property 'forEach' of undefined
    at https://forum.geekshubsacademy.com/embed/comments?embed_url=https%3A%2F%2Fgeekshubsacademy.com%2Flms%2Fintroduccion-a-swift-beta%2Fcontent%2F1%2F1%2F:203:34
Env

hostname	discourse-geekshubs-app
process_id	164
application_version	a74291c91a16596680ddd728b0ad20d30c67ee0c
HTTP_HOST	forum.geekshubsacademy.com
REQUEST_URI	/logs/report_js_error
REQUEST_METHOD	POST
HTTP_USER_AGENT	Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
HTTP_ACCEPT	*/*
HTTP_REFERER	https://forum.geekshubsacademy.com/embed/comments?embed_url=https%3A%2F%2Fgeekshubsacademy.com%2Flms%2Fintroduccion-a-swift-beta%2Fcontent%2F1%2F1%2F
HTTP_X_FORWARDED_FOR	81.44.35.48
HTTP_X_REAL_IP	81.44.35.48
params	
message	Uncaught TypeError: Cannot read property 'forEach' of undefined Url: https://forum.geekshubsacademy.c
url	https://forum.geekshubsacademy.com/embed/comments?embed_url=https%3A%2F%2Fgeekshubsacademy.com%2Flms%
line	203
column	34
window_location	https://forum.geekshubsacademy.com/embed/comments?embed_url=https%3A%2F%2Fgeekshubsacademy.com%2Flms%
stacktrace

Message (7 copies reported)

Job exception: 400 Bad Request
Backtrace

/usr/local/lib/ruby/2.3.0/open-uri.rb:359:in `open_http'
/usr/local/lib/ruby/2.3.0/open-uri.rb:737:in `buffer_open'
/usr/local/lib/ruby/2.3.0/open-uri.rb:212:in `block in open_loop'
/usr/local/lib/ruby/2.3.0/open-uri.rb:210:in `catch'
/usr/local/lib/ruby/2.3.0/open-uri.rb:210:in `open_loop'
/usr/local/lib/ruby/2.3.0/open-uri.rb:151:in `open_uri'
/var/www/discourse/lib/freedom_patches/open_uri_redirections.rb:70:in `open_uri'
/usr/local/lib/ruby/2.3.0/open-uri.rb:717:in `open'
/usr/local/lib/ruby/2.3.0/open-uri.rb:35:in `open'
/var/www/discourse/app/models/topic_embed.rb:79:in `find_remote'
/var/www/discourse/app/models/topic_embed.rb:116:in `import_remote'
/var/www/discourse/lib/topic_retriever.rb:56:in `fetch_http'
/var/www/discourse/lib/topic_retriever.rb:43:in `perform_retrieve'
/var/www/discourse/lib/topic_retriever.rb:10:in `retrieve'
/var/www/discourse/app/jobs/regular/retrieve_topic.rb:16:in `execute'
/var/www/discourse/app/jobs/base.rb:154:in `block (2 levels) in perform'
Env

hostname	discourse-geekshubs-app
process_id	112
application_version	a74291c91a16596680ddd728b0ad20d30c67ee0c
current_db	default
current_hostname	forum.geekshubsacademy.com
job	Jobs::RetrieveTopic
problem_db	default
opts	null
user_id	2
embed_url	https://geekshubsacademy.com/lms/introduccion-a-swift-beta/content/1/1/
referer	[https://geekshubsacademy.com/lms/introduccion-a-swift-beta/content/2/1, https://geekshubsacademy.com/lms/introduccion-a-swift-beta/content/1/1]
current_site_id	default

Message

TypeError: PreloadStore.get(...) is undefined
Url: https://forum.geekshubsacademy.com/embed/comments?embed_url=https%3A%2F%2Fgeekshubsacademy.com%2Flms%2Fintroduccion-a-swift-beta%2Fcontent%2F1%2F1%2F
Line: 201
Column: 2
Window Location: https://forum.geekshubsacademy.com/embed/comments?embed_url=https%3A%2F%2Fgeekshubsacademy.com%2Flms%2Fintroduccion-a-swift-beta%2Fcontent%2F1%2F1%2F
Backtrace

@https://forum.geekshubsacademy.com/embed/comments?embed_url=https%3A%2F%2Fgeekshubsacademy.com%2Flms%2Fintroduccion-a-swift-beta%2Fcontent%2F1%2F1%2F:201:3
Env

hostname	discourse-geekshubs-app
process_id	164
application_version	a74291c91a16596680ddd728b0ad20d30c67ee0c
HTTP_HOST	forum.geekshubsacademy.com
REQUEST_URI	/logs/report_js_error
REQUEST_METHOD	POST
HTTP_USER_AGENT	Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:40.0) Gecko/20100101 Firefox/40.0
HTTP_ACCEPT	*/*
HTTP_REFERER	https://forum.geekshubsacademy.com/embed/comments?embed_url=https%3A%2F%2Fgeekshubsacademy.com%2Flms%2Fintroduccion-a-swift-beta%2Fcontent%2F1%2F1%2F
HTTP_X_FORWARDED_FOR	193.145.225.170
HTTP_X_REAL_IP	193.145.225.170
params	
message	TypeError: PreloadStore.get(...) is undefined Url: https://forum.geekshubsacademy.com/embed/comments?
url	https://forum.geekshubsacademy.com/embed/comments?embed_url=https%3A%2F%2Fgeekshubsacademy.com%2Flms%
line	201
column	2
window_location	https://forum.geekshubsacademy.com/embed/comments?embed_url=https%3A%2F%2Fgeekshubsacademy.com%2Flms%
stacktrace	@https://forum.geekshubsacademy.com/embed/comments?embed_url=https%3A%2F%2Fgeekshubsacademy.com%2Flms

:frowning:


(Robin Ward) #4

I am a little confused!

You know what’s really odd is that highlighted_languages is not even part of the embedding process.

Can you access the embed script by just pointing your browser at it? For example here on meta you can visit https://meta.discourse.org/javascripts/embed.js and get the script. Does that work on your forum?


(Esther Martz) #5

Sure I can :slight_smile: Here’s the script:

(function() {

  var DE = window.DiscourseEmbed || {};
  var comments = document.getElementById('discourse-comments');
  var iframe = document.createElement('iframe');

  ['discourseUrl', 'discourseEmbedUrl', 'discourseUserName'].forEach(function(i) {
    if (window[i]) { DE[i] = DE[i] || window[i]; }
  });

  var queryParams = {};

  if (DE.discourseEmbedUrl) {
    queryParams.embed_url = encodeURIComponent(DE.discourseEmbedUrl);
  }

  if (DE.discourseUserName) {
    queryParams.discourse_username = DE.discourseUserName;
  }

  if (DE.topicId) {
    queryParams.topic_id = DE.topicId;
  }

  var src = DE.discourseUrl + 'embed/comments';
  var keys = Object.keys(queryParams);
  if (keys.length > 0) {
    src += "?";

    for (var i=0; i<keys.length; i++) {
      if (i > 0) { src += "&"; }

      var k = keys[i];
      src += k + "=" + queryParams[k];
    }
  }

  iframe.src = src;
  iframe.id = 'discourse-embed-frame';
  iframe.width = "100%";
  iframe.frameBorder = "0";
  iframe.scrolling = "no";
  comments.appendChild(iframe);

  // Thanks http://amendsoft-javascript.blogspot.ca/2010/04/find-x-and-y-coordinate-of-html-control.html
  function findPosY(obj)
  {
    var top = 0;
    if(obj.offsetParent)
    {
        while(1)
        {
          top += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    }
    else if(obj.y)
    {
        top += obj.y;
    }
    return top;
  }

  function normalizeUrl(url) {
    return url.replace(/^https?(\:\/\/)?/, '');
  }

  function postMessageReceived(e) {
    if (!e) { return; }
    if (normalizeUrl(DE.discourseUrl).indexOf(normalizeUrl(e.origin)) === -1) { return; }

    if (e.data) {
      if (e.data.type === 'discourse-resize' && e.data.height) {
        iframe.height = e.data.height + "px";
      }

      if (e.data.type === 'discourse-scroll' && e.data.top) {
        // find iframe offset
        var destY = findPosY(iframe) + e.data.top;
        window.scrollTo(0, destY);
      }
    }
  }
  window.addEventListener('message', postMessageReceived, false);

})();

(Robin Ward) #6

So that’s all good. I’m wondering then if the content it’s getting back is not embedded for some reason. Perhaps your site is set to invite only, or requires a login?

Is there any security setting that would prevent the tropic from being viewed publicly?


(Esther Martz) #7

Yep, at the moment its only accessible by invitation and the category to be embeded is only accessible by one group.

Edit: Tried disabling these options but remains the same :frowning:

Edit2: This is what I get when trying to embed:


(Robin Ward) #8

That looks correct actually. You’ll want to view it as anonymous (logged out) to see what a non-admin sees, but that’s a “start discussion” button and your logo.

If you want to get rid of that debug information in the corner you can visit your forum with ?pp=disable in the URL.


(Esther Martz) #9

Got rid of the admin info debug, and there’s a “Loading conversation” message. Once loaded, it only displays my forum header :frowning:

Edit: Actually, my backend colleague is reporting the same error when using the discourse Api for getting the user notifications. Here’s his error log:

18:20
uncaughtException: Cannot read property ‘forEach’ of undefined date=Thu Jun 16 2016 12:16:45 GMT-0400 (EDT), pid=4088, uid=0, gid=0, cwd=/root/api-rest-academy-test, execPath=/usr/local/bin/node, version=v4.2.4, argv=[/usr/local/bin/node, /root/api-rest-academy-test/app.js], rss=74600448, heapTotal=56788080, heapUsed=53826744, loadavg=[0.021484375, 0.0341796875, 0.04541015625], uptime=12373433, trace=[column=36, file=/root/api-rest-academy-test/api/lms/v1_0/controladores/feedsControler.js, function=null, line=49, method=null, native=false, column=17, file=/root/api-rest-academy-test/api/lms/v1_0/servicios/discourseApi.js, function=Request._callback, line=67, method=_callback, native=false, column=22, file=/root/api-rest-academy-test/node_modules/request/request.js, function=Request.self.callback, line=200, method=self.callback, native=false, column=13, file=events.js, function=emitTwo, line=87, method=null, native=false, column=7, file=events.js, function=Request.emit, line=172, method=emit, native=false, column=10, file=/root/api-rest-academy-test/node_modules/request/request.js, function=, line=1067, method=null, native=false, column=20, file=events.js, function=emitOne, line=82, method=null, native=false, column=7, file=events.js, function=Request.emit, line=169, method=emit, native=false, column=12, file=/root/api-rest-academy-test/node_modules/request/request.js, function=, line=988, method=null, native=false, column=20, file=events.js, function=emitNone, line=72, method=null, native=false, column=7, file=events.js, function=IncomingMessage.emit, line=166, method=emit, native=false, column=12, file=_stream_readable.js, function=endReadableNT, line=905, method=null, native=false, column=9, file=node.js, function=doNTCallback2, line=441, method=null, native=false, column=17, file=node.js, function=process._tickCallback, line=355, method=_tickCallback, native=false], stack=[TypeError: Cannot read property ‘forEach’ of undefined, at /root/api-rest-academy-test/api/lms/v1_0/controladores/feedsControler.js:49:36, at Request._callback (/root/api-rest-academy-test/api/lms/v1_0/servicios/discourseApi.js:67:17), at Request.self.callback (/root/api-rest-academy-test/node_modules/request/request.js:200:22), at emitTwo (events.js:87:13), at Request.emit (events.js:172:7), at Request.< anonymous> (/root/api-rest-academy-test/node_modules/request/request.js:1067:10), at emitOne (events.js:82:20), at Request.emit (events.js:169:7), at IncomingMessage.< anonymous> (/root/api-rest-academy-test/node_modules/request/request.js:988:12), at emitNone (events.js:72:20), at IncomingMessage.emit (events.js:166:7), at endReadableNT (_stream_readable.js:905:12), at doNTCallback2 (node.js:441:9), at process._tickCallback (node.js:355:17)]

Edit: We’re using the DigitalOcean autoinstall.