ROR-咖啡脚本导致“;未捕获引用错误”;

ROR - coffeescript causing "Uncaught ReferenceError"

本文关键字:引用 错误 ROR- 脚本 咖啡      更新时间:2023-09-26

我收到的错误令人费解,因为我从一个控制器/views/javascript中复制了一些正在进行名称更改的工作代码,并删除了行以获得初始工作版本。导致运行时错误的coffeescript文件的相关部分如下。

app/assets/javascripts/companyes.js.coffee

...
calculateResult = (company_id)-> 
  data = $('#x_company_drill_interests').serialize()
  console.log(" -01- in calculateResult ")
  console.log data
  $.ajax
    url:"/companies/#{company_id}/projection.json",
    type:"post"
    dataType: 'json'   # data type of response
    data: data
    failure: (data,success,xhr)->
       console.log(" -01- in calculateResult - Failure ")
       console.log data
    success: (data,success,xhr)->
       console.log("-01- in calculateResult - SUCCESS ")
       company_listings_block = $ '#x_company_listings_results .infogroup-body'
       head_row = $ '''
<tr>
  <th>Company</th>
</tr>
'''
      table = $ '<table border="0" cellpadding="0" cellspacing="5"></table>'
      table
        .addClass('info')
        .append(head_row)
      for result in data
        name = result.display_name
        result_row = $ """
<tr>
  <td>#{name}</td>
</tr>
"""
        table.append result_row
      eval_result_block.html('').append table

产生错误:

未获取ReferenceError:未定义head_row

这是由线路CCD_ 1引起的。如果我删除这一行,我会得到这个错误Uncaught ReferenceError: eval_result_block is not defined

同样由于某些原因,我无法获得使用Post路由的代码。这就是我添加以修改路线文件的方式。

config/rutes.rb

resources :companies, only: [:destroy, :update] do
  member do
    get 'companies_drill_interests'
    match 'projection', via: [:get,:post, :patch]
  end
end

这里是我从复制的工作代码,供参考

calculateResult = (drill_id)->
  console.log("  -001- in calculate results")
  data = $('#x_evaluation_assumption_params').serialize()
  $.ajax
    url:"/drills/#{drill_id}/projection.json",
    type:"post"
    dataType: 'json'   # data type of response
    data: data
    failure: (data,success,xhr)->
      console.log("     -001- in Calculate Result - Failure ")
      console.log data
    success: (data,success,xhr)->
      # console.log("     -001-     print data from call")
      eval_result_block = $ '#x_id_evaluation_results .infogroup-body'
      head_row = $ '''
<tr>
  <th>Company</th>
  <th>Price</th>
  <th>Mkt Cap</th>
  <th>Discovery Value</th>
  <th>Target Price</th>
  <th>Leverage</th>
  <th>Risked Lev </th>
  <th>Leverage with CFD's</th>
</tr>
'''
      table = $ '<table border="0" cellpadding="0" cellspacing="5"></table>'
      table
        .addClass('info')
        .append(head_row)
      for result in data
        if !(result.listing.option_unlisted) 
          name = result.display_name
          share_price = '$' +
            NumberHelpers.number_with_precision((result.listing.share_price/1000), {separator: '.', precision: 3, delimiter: ','})
          market_capitalisation_mill = '$' +                 
             NumberHelpers.number_with_precision((result.market_capitalisation/1000000), {separator: '.', precision: 1, delimiter: ','}) + 'M'
          discovery_value = '$' + 
            NumberHelpers.number_with_precision(result.discovery_value_total, {separator: '.', precision: 0, delimiter: ','})
          discovery_value_per_share = '$' + 
            NumberHelpers.number_with_precision((result.target_share_price), {separator: '.', precision: 2, delimiter: ','})
          leverage = 
        NumberHelpers.number_with_precision(result.leverage, {separator: '.', precision: 0, delimiter: ','}) + '%'
      risked_leverage =  
            NumberHelpers.number_with_precision(result.risked_leverage, {separator: '.', precision: 0, delimiter: ','}) + '%'
          leverage_with_CFD = 
            NumberHelpers.number_with_precision(result.leverage_with_CFD, {separator: '.', precision: 0, delimiter: ','}) + '%'
          result_row = $ """
<tr>
  <td>#{name}</td>
  <td>#{share_price}</td>
  <td>#{market_capitalisation_mill}</td>
  <td>#{discovery_value}</td>
  <td>#{discovery_value_per_share}</td>
  <td>#{leverage}</td>
  <td>#{risked_leverage}</td>
  <td>#{leverage_with_CFD}</td>
</tr>
"""
        table.append result_row
      eval_result_block.html('').append table

在第一个代码中,eval_result_block在使用之前没有定义。关于result_row,在代码的第一部分中,它进入了for循环,而在代码的另一部分中,由于risked_leverage具有不同的缩进,它在for循环之外,缩短了4个空格。这意味着它在for循环之外,所以,这就是为什么result_row也在外部。

在coffeescript中,缩进很重要。如果您放置3个空格而不是2个空格,则生成的代码将有所不同。查看您的代码:

failure: (data,success,xhr)->
   console.log(" -01- in calculateResult - Failure ")
   console.log data
success: (data,success,xhr)->
   console.log("-01- in calculateResult - SUCCESS ")
   company_listings_block = $ '#x_company_listings_results .infogroup-body'
   head_row = $ '''

如果不同

failure: (data,success,xhr)->
  console.log(" -01- in calculateResult - Failure ")
  console.log data
success: (data,success,xhr)->
  console.log("-01- in calculateResult - SUCCESS ")
  company_listings_block = $ '#x_company_listings_results .infogroup-body'
  head_row = $ '''

因为在第一种情况下有3个空间,而在第二种情况下则有2个空间。

这是带有正确缩进的代码。通过阅读您的代码,我认为.append(head_row)0和table.append result_row必须在for循环中。

calculateResult = (company_id)-> 
  data = $('#x_company_drill_interests').serialize()
  console.log(" -01- in calculateResult ")
  console.log data
  $.ajax
    url:"/companies/#{company_id}/projection.json",
    type:"post"
    dataType: 'json'   # data type of response
    data: data
    failure: (data,success,xhr)->
      console.log(" -01- in calculateResult - Failure ")
      console.log data
    success: (data,success,xhr)->
      console.log("-01- in calculateResult - SUCCESS ")
      company_listings_block = $ '#x_company_listings_results .infogroup-body'
      head_row = $ '''
<tr>
  <th>Company</th>
</tr>
'''
      table = $ '<table border="0" cellpadding="0" cellspacing="5"></table>'
      table
        .addClass('info')
        .append(head_row)
      for result in data
        name = result.display_name
        result_row = $ """
<tr>
  <td>#{name}</td>
</tr>
"""
        table.append result_row
      company_listings_block.html('').append table

您可以尝试将代码的每一部分编译成javascript,这将帮助您了解发生了什么。咖啡编译器