gpt4 book ai didi

javascript - ROR - coffeescript 导致 "Uncaught ReferenceError"

转载 作者:行者123 更新时间:2023-11-29 19:34:33 24 4
gpt4 key购买 nike

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

应用程序/ Assets /javascripts/companies.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

产生错误:

uncaught ReferenceError: head_row is not defined

这是由 .append(head_row) 行引起的。如果删除此行,我会收到此错误 Uncaught ReferenceError: eval_result_block is not defined

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

配置/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循环,而在代码的另一部分中,它位于此 for 之外因为 risked_leverage 循环有不同的缩进,短了 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 个空格。

此处您的代码具有正确的缩进。通过阅读你的代码,我认为 result_rowtable.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,这将有助于您理解发生了什么。 Coffee compiler

关于javascript - ROR - coffeescript 导致 "Uncaught ReferenceError",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25776912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com