【问题标题】:Why const someValue = this.props.dispatch(someMethod()) is working fine on localhost but not in production?为什么 const someValue = this.props.dispatch(someMethod()) 在 localhost 上运行良好,但在生产中却不行?
【发布时间】:2019-04-17 09:57:30
【问题描述】:

我有一个名为 onChange 的方法,它在本地主机上按预期工作,但在生产环境中却没有。查看下面的代码以更好地理解:

onChange() {
   const someValue = this.props.dispatch(divideTenByFive()) // returns 2 on both local and prod
   console.log(someValue) // prints 2 on localhost and undefined on production
   console.log(this.props.dispatch(divideTenByFive())) // prints 2 on localhost and undefined on production
   return someValue; 
}

它与同步/异步编程有关吗?如何在生产中解决这个问题?

注意:我无法分享实际的业务逻辑,因此我编写了 divideTenByFive() 以便更好地理解。但是,我已尝试提供最大的详细信息,但如果您正在寻找更多信息,请在下面发表评论。

更新:我没有使用任何中间件

【问题讨论】:

  • 你是否使用了一些中间件来返回值
  • @ShubhamKhatri 不,我没有使用任何中间件来返回值。很简单的 try catch 和 if else 在 divideTenByFive() 中进行,它也会返回生产中的预期值。
  • 如果您需要帮助,需要完整的工作代码而不是 3 行代码。最好在任何开源网站上,例如 GitHub。

标签: javascript reactjs ecmascript-6 redux react-redux


【解决方案1】:

您能否尝试使用 var instead 的 const 来声明变量。

一旦您设置了它(在开发中设置为 2,在生产中设置为“未定义”),它就无法更改。

这是因为该值尚不可用,但 const 将阻止以后进行任何修改。

【讨论】:

  • 我尝试过使用 var 代替 const,但它不能解决这个问题。
  • 您能否尝试将其设为全局变量并将其传递给您的函数..
  • 您可以通过省略 var / const 关键字使其成为全局变量。
猜你喜欢
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多