undefined|0|ReferenceError:严格模式禁止隐式创建全局属性“csrf_token" [英] undefined|0|ReferenceError: Strict mode forbids implicit creation of global property 'csrf_token'
问题描述
所以,这是我遇到的一个非常有趣的问题.
So, this was quite an interesting problem I have been running into.
我目前正在构建一个backbone.js - Rails 应用程序.通常只是为了学习目的而构建它.我(就像任何优秀的 Rails 开发人员一样)在 TDD/BDD 方面尽我所能,但我在使用水豚时遇到了问题.
I am currently building a backbone.js - Rails app. Generally just building this for learning purposes. I am (like any good rails dev) doing my best at TDD/BDD and I ran into a problem with capybara.
我有一个只测试 root_path 工作的集成规范(主干历史开始,显示初始信息等......).
I have an integration spec that merely tests root_path works (Backbone history starts, displays initial information, etc...).
require 'spec_helper'
describe "RentalProperties", js: true do
describe "GET /" do
it "should show a list of properties" do
visit root_path
eventually{page.should have_content("Something")}
end
end
end
我正在用 jasmine、sinon 和 capybara/rspec/webkit 运行测试.我松散地关注了thoughtbot 的Rspec on Rails"一书(顺便说一句很棒的书)和本教程:http://tinnedfruit.com/2011/03/03/testing-backbone-apps-with-jasmine-sinon.html.
I am running tests with jasmine, sinon, and capybara/rspec/webkit. I am loosely following both the "Rspec on Rails" book by thoughtbot (awesome book by the way), and this tutorial: http://tinnedfruit.com/2011/03/03/testing-backbone-apps-with-jasmine-sinon.html.
在运行上述规范时,我遇到了这个错误:
When running the above spec, I came across this error:
undefined|0|ReferenceError: Strict mode forbids implicit creation of global property 'csrf_token'
我花了很长时间才解决这个问题,因为这个错误真的没有谷歌可以解决的问题.
I took a long time sorting this out because there's really nothing google-able for this error.
最终我偶然发现在 JS 中使用了使用严格模式".本质上,这将使用一些新的 EMCA5 脚本约定.它将捕获更多的编码漏洞,并阻止您访问全局变量.都是好东西.
Eventually I stumbled across using "use strict-mode" in JS. Essentially this will use some new EMCA5 script conventions. It will catch more coding bloopers, and keep you from accessing global variables. All good things.
所以我检查,在我的 sinon.js 文件中,我看到:
So I check, and in my sinon.js file, I see:
"use strict";
在文件的第 36 行.瞧,我注释掉了该行,我的测试工作正常.
on line 36 of the file. Lo and behold I comment out the line, and my tests work just fine.
这是我的问题:为什么使用严格搞乱 csrf?我假设这与我的 rails 布局中的 csrf_meta_tags 有关.如果可能的话,我想将此行放回 sinon js 中,因为我认为它是正确的做法"
Here is my question: Why did use strict mess up csrf? I am assuming this has something to do with csrf_meta_tags in my rails layout. If possible I would like to put this line back in sinon js as I assume its the "right thing to do"
有没有人有这方面的更多信息?我提前感谢任何细节!
Does anyone have more information on this? I appreciate any details in advance!!
推荐答案
它告诉您正在将一个值分配给一个名为 csrf_token
的变量,该变量尚未声明,例如
It is telling you that a value is being assigned to a variable called csrf_token
that has not been declared, e.g.
csrf_token = 'foo';
在非严格模式下,这将在执行该行代码时创建名为 csrf_token
的全局对象(通常称为全局变量)的属性.
In non–strict mode, that will create a property of the global object (usually called a global variable) called csrf_token
when that line of code is executed.
在严格模式下,它会抛出你看到的错误,因为严格模式阻止了全局变量的隐式创建.您还可以通过包含以下内容来修复它:
In strict mode, it will throw the error you see because strict mode prevents the implicit creation of global variables. You could also fix it by including:
var csrf_token;
在与错误来自的代码相同的脚本元素中的全局上下文中的任何位置,或前一个脚本元素.
anywhere in a global context in the same script element as the code the error comes from, or a previous script element.
这篇关于undefined|0|ReferenceError:严格模式禁止隐式创建全局属性“csrf_token"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!