javascript undefined|0|ReferenceError:严格模式禁止隐式创建全局属性“csrf_token”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13794637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:48:44  来源:igfitidea点击:

undefined|0|ReferenceError: Strict mode forbids implicit creation of global property 'csrf_token'

javascriptruby-on-railsbackbone.jsjasminesinon

提问by cpow

So, this was quite an interesting problem I have been running into.

所以,这是我一直遇到的一个非常有趣的问题。

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.

我目前正在构建一个backbone.js - Rails 应用程序。通常只是为了学习目的而构建它。我(就像任何优秀的 Rails 开发人员一样)在 TDD/BDD 方面做得最好,但我遇到了水豚的问题。

I have an integration spec that merely tests root_path works (Backbone history starts, displays initial information, etc...).

我有一个仅测试 root_path 工作的集成规范(主干历史开始,显示初始信息等......)。

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

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.

我正在用 jasmine、sinon 和 capybara/rspec/webkit 运行测试。我松散地关注了thoughtbot的“Rspec on Rails”一书(顺便说一句很棒的书)和本教程: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.

我花了很长时间才解决这个问题,因为这个错误真的没有谷歌可以解决的问题。

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.

最终我偶然发现在 JS 中使用“使用严格模式”。本质上,这将使用一些新的 EMCA5 脚本约定。它将捕获更多的编码漏洞,并阻止您访问全局变量。一切美好的事物。

So I check, and in my sinon.js file, I see:

所以我检查,在我的 sinon.js 文件中,我看到:

"use strict";

on line 36 of the file. Lo and behold I comment out the line, and my tests work just fine.

在文件的第 36 行。瞧,我注释掉了这一行,我的测试工作得很好。

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"

这是我的问题:为什么使用严格搞乱 csrf?我假设这与我的 rails 布局中的 csrf_meta_tags 有关。如果可能的话,我想将此行放回 sinon js 中,因为我认为它是“正确的做法”

Does anyone have more information on this? I appreciate any details in advance!!

有没有人有更多关于这方面的信息?我提前感谢任何细节!

回答by RobG

It is telling you that a value is being assigned to a variable called csrf_tokenthat has not been declared, e.g.

它告诉你一个值被分配给一个csrf_token尚未声明的变量,例如

csrf_token = 'foo';

In non–strict mode, that will create a property of the global object (usually called a global variable) called csrf_tokenwhen that line of code is executed.

在非严格模式下,这将创建csrf_token执行该行代码时调用的全局对象(通常称为全局变量)的属性。

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.

与错误来自的代码或前一个脚本元素相同的脚本元素中的全局上下文中的任何位置。