【问题标题】:TypeError: Cannot read property 'fraction' of undefined - Cypress/JavascriptTypeError:无法读取未定义的属性“分数”-赛普拉斯/Javascript
【发布时间】:2021-09-13 05:55:48
【问题描述】:

我正在做一个测试,我获取一个赔率值并将其存储为分数。然后我想根据从下拉列表中选择的选项将该值与特定格式进行比较。

我正在尝试使用这些来设置分数并确定赔率格式:

https://mathjs.org/docs/datatypes/fractions.html

如何解决上述类型错误。

截图:

【问题讨论】:

  • 你应该只问一个问题。
  • 啊,不用担心,我将问题更新为仅包含 1 个问题 :)
  • 你确定 mathjs import 出了什么问题了吗?该错误意味着mathundefined
  • @Pointy 所以我知道 npm 安装很好,看起来它导入的 mathjs 很好,但不知道为什么未定义。我在上面附上了截图
  • 如果 mathjs 已经“正常”导入,那么 math 将不是 undefined

标签: javascript cypress


【解决方案1】:

mathjs 没有默认导出,因此您导入所有内容 (*),然后在导入中命名

import * as math from 'mathjs'

这是mathjs 包的主要入口点。

// configuration
export { config } from './configReadonly.js'

// functions and constants
export * from './pureFunctionsAny.generated.js'
export * from './impureFunctionsAny.generated.js'
export * from './typeChecks.js'

// error classes
export { IndexError } from '../error/IndexError.js'
export { DimensionError } from '../error/DimensionError.js'
export { ArgumentsError } from '../error/ArgumentsError.js'

// dependency groups
export * from './dependenciesAny.generated.js'

// factory functions
export * from '../factoriesAny.js'

// core
export { create } from '../core/create.js'
export { factory } from '../utils/factory.js'

如果它有一个默认导出,就会有

export default {
  function1,
  function2,
  function3,
  ...
}

然后你可以不使用* as math来导入它。

【讨论】:

    【解决方案2】:

    如果您只想使用fraction,您可以使用:

    const { fraction } = require("mathjs");
    
    //In your test
    fraction(oddsValue);
    

    如果你也想使用其他人,你可以这样做:

    import { create, all } from "mathjs"
    const math = create(all, {})
    
    //In your test
    math.fraction(oddsValue)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多