CB tutorial: Uncaught TypeError: Cannot read property 'length' of undefined

433 views
Skip to first unread message

Romu

unread,
Jan 14, 2014, 5:32:50 AM1/14/14
to chica...@googlegroups.com
Hi,

I was trying the example in CB tutorial "3. Implementing real-time updates with save hooks", below is my live.html:

<html><head>
<title>Fresh hot greetings!</title>
<script>
  function listen_for_events(timestamp) {
    $.ajax("/greeting/pull/"+timestamp, { success:
      function(data, code, xhr) {
        console.log(typeof data.greetings);
        console.log(typeof data.greetings !== 'undefined' ? data.greetings.length : 'There is no spoon.');
        for (var i=0; i<data.greetings.length; i++) {
          var msg = data.greetings[i].greeting_text;
          $("#greeting_list").append("<li>"+msg);
        }
        listen_for_events(data.timestamp);
      } });
  }
  $(document).ready(function() {
    listen_for_events({{ timestamp }});
  });
</script>
</head>
<body>
  <ul id="greeting_list">
  {% for greeting in greetings %}
    <li>{{ greeting.greeting_text }}
  {% empty %}
    <li>No greetings!
  {% endfor %}
</ul>
</body>
</html>

When I create a new greeting, the live page does not update with the new greeting, in console I see javascript error:

Uncaught TypeError: Cannot read property 'length' of undefined live:10
$.ajax.success        live:10
c                            jquery.js:3048
p.fireWith                jquery.js:3160
k                            jquery.js:8235
r                             jquery.js:8778

Actually 'data' is undefined here.

Browser is Chrome 32.0.1700.72 m.  I also tried jquery 1.70 and 2.0.3, same issue.

Any idea?


Thanks
Romu

can2nac

unread,
Jan 14, 2014, 9:05:09 AM1/14/14
to chica...@googlegroups.com
Actually 'data' is undefined here. --- i think this is the key, post controller here.

Romu

unread,
Jan 14, 2014, 9:35:37 PM1/14/14
to chica...@googlegroups.com
The controller (cb_tutorial_greeting_controller.erl):

-module(cb_tutorial_greeting_controller, [Req]).
-compile(export_all).

hello('GET', []) ->
    {ok, [{greeting, "Hello, world!"}]}.

list('GET', []) ->
    Greetings = boss_db:find(greeting, []),
    {ok, [{greetings, Greetings}]}.

create('GET', []) ->
    ok;
create('POST', []) ->
    GreetingText = Req:post_param("greeting_text"),
    NewGreeting = greeting:new(id, GreetingText),
    case NewGreeting:save() of
        {ok, SavedGreeting} ->
            {redirect, [{action, "list"}]};
        {error, ErrorList} ->
            {ok, [{errors, ErrorList}, {new_msg, NewGreeting}]}
    end.

goodbye('POST', []) ->
    boss_db:delete(Req:post_param("greeting_id")),
    {redirect, [{action, "list"}]}.

send_test_message('GET', []) ->
    TestMessage = "Free at last!",
    boss_mq:push("test-channel", TestMessage),
    {output, TestMessage}.

pull('GET', [LastTimestamp]) ->
    {ok, Timestamp, Greetings} = boss_mq:pull("new-greetings",
        list_to_integer(LastTimestamp)),
    {json, [{timestamp, Timestamp}, {greetings, Greetings}]}.

live('GET', []) ->
    Greetings = boss_db:find(greeting, []),
    Timestamp = boss_mq:now("new-greetings"),
    {ok, [{greetings, Greetings}, {timestamp, Timestamp}]}.


And the model (greeting.erl):

-module(greeting, [Id, GreetingText]).
-compile(export_all).

validation_tests() ->
    [{fun() -> length(GreetingText) > 0 end,
        "Greeting must be non-empty!"},
     {fun() -> length(GreetingText) =< 140 end,
        "Greeting must be tweetable"}].

before_create() ->
    ModifiedRecord = set(greeting_text,
                         re:replace(GreetingText,
                                    "masticate", "chew",
                                    [{return, list}])),
    {ok, ModifiedRecord}.

after_create() ->
    boss_mq:push("new-greetings", THIS).


Thanks
Romu

can2nac於 2014年1月14日星期二UTC+8下午10時05分09秒寫道:
Message has been deleted

can2nac

unread,
Jan 15, 2014, 5:30:36 PM1/15/14
to chica...@googlegroups.com
check console, there are must be an error, at least i have one

23:50:49.116 [error] {function_clause,[{boss_json,json_data1,[[{greeting,"greeting-16","2"}],[],[]],[{file,"src/boss/boss_json.erl"},{line,23}]},{boss_json,json_data1,3,[{file,"src/boss/boss_json.erl"},{line,32}]},{boss_json,encode,2,[{file,"src/boss/boss_json.erl"},{line,9}]},{boss_web_controller,process_action_result,4,[{file,"src/boss/boss_web_controller.erl"},{line,1050}]},{boss_web_controller,execute_action,4,[{file,"src/boss/boss_web_controller.erl"},{line,882}]},{boss_web_controller,process_dynamic_request,4,[{file,"src/boss/boss_web_controller.erl"},{line,521}]},{boss_web_controller,process_request,4,[{file,"src/boss/boss_web_controller.erl"},{line,510}]},{timer,tc,3,[{file,"timer.erl"},{line,194}]}]}

if u have the same, then go to boss_json and check it.

best

On Tuesday, January 14, 2014 12:32:50 PM UTC+2, Romu wrote:

can2nac

unread,
Jan 15, 2014, 5:33:56 PM1/15/14
to chica...@googlegroups.com
also there is some helping info in server response


<html>
    <head>
        <title>Oops!</title>
        <link rel="stylesheet" type="text/css" href="/static/edoc/stylesheet.css" title="EDoc">
    </head>
    <body>
    <div class="container">
        <h1>We got a problem!</h1>
        <div class="alert alert-warning">This message will appear in production; you may want to define a 500 handler in /home/can2nac/projects/erlang/cb_tutorial/priv/cb_tutorial.routes</div>
        <h2>Error:</h2>
        <pre>{function_clause,
    [{boss_json,json_data1,
         [[{greeting,"greeting-31","test message\r\n"}],[],[]],
         [{file,"src/boss/boss_json.erl"},{line,23}]},
     {boss_json,json_data1,3,[{file,"src/boss/boss_json.erl"},{line,32}]},
     {boss_json,encode,2,[{file,"src/boss/boss_json.erl"},{line,9}]},
     {boss_web_controller,process_action_result,4,
         [{file,"src/boss/boss_web_controller.erl"},{line,1050}]},
     {boss_web_controller,execute_action,4,
         [{file,"src/boss/boss_web_controller.erl"},{line,882}]},
     {boss_web_controller,process_dynamic_request,4,
         [{file,"src/boss/boss_web_controller.erl"},{line,521}]},
     {boss_web_controller,process_request,4,
         [{file,"src/boss/boss_web_controller.erl"},{line,510}]},
     {timer,tc,3,[{file,"timer.erl"},{line,194}]}]}</pre>
        <h2>Request:</h2>
        <pre class=".pre-scrollable">{simple_bridge_request_wrapper,mochiweb_request_bridge,
    {mochicow_request,#Port<0.61244>,'GET',"/greeting/pull/1389825103565229",
        {1,1},
        {10,
         {"host",
          {"host","localhost:8001"},
          {"connection",
           {"connection","keep-alive"},
           {"cache-control",
            {"cache-control","max-age=0"},
            {"accept",
             {"accept","*/*"},
             nil,
             {"accept-encoding",
              {"accept-encoding","gzip,deflate,sdch"},
              nil,
              {"accept-language",
               {"accept-language","ru,en-US;q=0.8,en;q=0.6"},
               nil,nil}}},
            nil},
           {"dnt",{"dnt","1"},nil,nil}},
          {"user-agent",
           {"user-agent",
            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/31.0.1650.63 Chrome/31.0.1650.63 Safari/537.36"},
           {"referer",
            {"referer","http://localhost:8001/greeting/live"},
            nil,nil},
           {"x-requested-with",
            {"x-requested-with","XMLHttpRequest"},
            nil,nil}}}},
        <<>>},
    false,[],[],none}</pre>
        <h2>Application info:</h2>
        <pre class=".pre-scrollable">{boss_app_info,cb_tutorial,[],"/static","/doc",all,
               [{cb_tutorial_01_news,[]}],
               <0.348.0>,<0.349.0>,<0.351.0>,<0.352.0>,[],[],
               ["cb_tutorial_greeting_controller"]}</pre>
        <p><em>Thu, 16 January 2014 12:31 a.m.</em></p>
    </div>
    </body>




On Tuesday, January 14, 2014 12:32:50 PM UTC+2, Romu wrote:

Romu

unread,
Jan 15, 2014, 10:23:19 PM1/15/14
to chica...@googlegroups.com
I don't see this in my console.  Are you suggesting that this may be caused by a bug in boss_json?

Thanks
Romu

can2nac於 2014年1月16日星期四UTC+8上午6時30分36秒寫道:

Romu

unread,
Jan 15, 2014, 10:28:19 PM1/15/14
to chica...@googlegroups.com
I don't see this.  The URL can't be opened.

BTW, I also tried the code with CB 0.8.9 and no luck.

Thanks
Romu

can2nac於 2014年1月16日星期四UTC+8上午6時33分56秒寫道:
Reply all
Reply to author
Forward
0 new messages