【问题标题】:Unexpected token ) in my code意外的令牌)在我的代码中
【发布时间】:2015-08-12 21:44:53
【问题描述】:

我正在学习 canjs 并尝试检索数据。因为我是初学者,所以我将整个代码写在一个文件中。

这是我的 canjs 文件:

 Players = can.Control({
init: function(){
    this.element.html(can.view('view/players.ejs',{
        players: this.options.players
    }));
  }
})


Player = can.Model({
  findAll: 'GET /players'
},{});

var PLAYERS = [
 {


"id" : 1,
"name" : "Dipesh",
"rank" : 2,
"score" : 2000,
"__v" : 0

},{

"id" : 2,
"name" : "Aakanksha",
"rank" : 3,
"score" : 3920,
"__v" : 0
}];

can.fixture('GET /players', function(){
  return [PLAYERS];
});
$(document).ready(function(){
 $.when(Player.findAll()).then(
   function(playersResponse){
    var players = playersResponse[0]


     new Players('.player', {
        players: players

    });
  });
});

这是我的 ejs 模板:

<ul id="sidebar">
   <% list(players, function(player){ %>
     <li class="player" <%=(el)-> el.data('player', player) %>>
      <%== can.view.render('playerView.ejs', {
                player:player

            }) 
        %>

    </li>
<% }) %>

在运行文件时,它在 jquery 中显示错误 Uncaught SyntaxError: Unexpected token )

但这怎么可能呢?我没有在 jquery 中更改任何内容。

【问题讨论】:

  • 这是在谈论您的代码,而不是 jquery 库:D
  • 可能是关于它出现的行号的错误..?
  • @DemoUser 它指向 jquery 库。我什至不打开它
  • 如果您不明白如何描述它,请发布显示错误的控制台屏幕截图:)
  • 将文件链接到 jQuery 的位置,删除该链接并使用此链接 &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"&gt;&lt;/script&gt; ....问题仍然存在吗?

标签: javascript jquery ejs canjs


【解决方案1】:

你一定是错过了什么

$(document).ready(function(){
    $.when(Player.findAll()).then(
        function(playersResponse){
            var players = playersResponse[0];

            // Try to add a semicolon (;) at the end of playersResponse[0]
            //Hope this should help you

            new Players('.player', {
                players: players

            });
        });
});

更新:尝试在 playerResponse[0] 末尾添加分号 (;)。希望这对您有所帮助。

【讨论】:

  • 只是好奇,你为什么这么说?
  • 哦,是的,绝对如此。我只是好奇你是怎么知道这段代码的
  • 我在这里学习。 (code.tutsplus.com/tutorials/diving-into-canjs--net-25105) 上面页面的那个块中没有给出任何内容
  • 尝试在 playerResponse[0] 末尾添加分号 (;)。希望这对您有所帮助。
  • 去做并告诉我。
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2023-01-19
  • 1970-01-01
  • 2016-12-14
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
相关资源
最近更新 更多