【问题标题】:Stuck with the "Expected an assignment or function: no-unused expressions"卡在“预期的赋值或函数:没有未使用的表达式”
【发布时间】:2019-12-09 21:41:25
【问题描述】:

我真的快疯了,希望有人能帮我解决这个问题。 我的模型中有以下代码:

this.text = json.text ? json.text : ''

这会在我的检查器中发送以下警告

期待一个赋值或函数调用,却看到一个表达式 no-unused-expressions

所以我尝试了以下方法:

let text;
if(json.text) text = json.text
else text = ''
this.text = text

但奇怪的是,这仍然返回相同的警告。

有人可以解释一下为什么会出现这个警告以及我应该怎么做才能解决这个问题吗?我想要一个无警告的网络应用程序。

更新: 我清理了我的代码,让那些不在项目中的人更容易理解它。 这是相互反应的两个组件。 对于从查询中获得的每个项目,我都会转到模型中的 fromJSON 方法。我将返回的值推送到我的结果数组中并返回该数组。

模型.js

import BaseModel from './BaseModel'

export default class Model extends BaseModel {
    constructor(){
        super()
        this.nis
        this.postal
        this.text
        this.type
    }

    fromJSON(json) {
        this.nis = json.nisCode ? json.nisCode : ''
        this.postal = json.postal ? json.postal : ''
        this.text = json.text ? json.text : ''
        this.type = json.type ? json.type : ''
    }
}

Service.js

import { BaseService } from './BaseService';
import Model from '../models/Model';
import Translation from '../components/_translations/Meta';

// To get content in correct language
const lang = new Translation().getContent('lang')

export default class Service extends Service {
    async search(keyword, type = 'keyword', query) {
        // Get the results data using the Axios instance
        return this.axiosInstanceOsn.get(query).then(
            value => {
                // First and foremost, check if the call was successful
                if (value.status === 200){

                    const data = value.data
                    let results = []
                    data.forEach(element => {
                        let as = new Model()
                        as.fromJSON(element)
                        results.push(as)
                    })
                    return results
                }
            }
        )
    }
}

【问题讨论】:

  • 难道this.text 从未使用过?
  • 不要在问题的标题中加上“标签”——这就是 标签 的用途。
  • 能否在初始化文本变量的地方添加更清晰的代码
  • 我用我的模型和服务的完整代码更新了我的问题
  • 在您的模型中,未使用的表达式是构造函数中无意义的this.nis 等;根据eslint.org/demo fromJSON 方法很好。通常,“备用构造函数”将由静态方法提供,因此您不会在未初始化状态下构造对象。

标签: javascript reactjs expression assignment-operator


【解决方案1】:

更新 2

import BaseModel from './BaseModel'

export default class Model extends BaseModel {
    constructor(){
        super()
        this.nis = ''
        this.postal = ''
        this.text = ''
        this.type = ''
    }

    fromJSON(json) {
        this.nis = (json.nisCode !== undefined ? json.nisCode : '')
        this.postal = (json.postal !== undefined ? json.postal : '')
        this.text = (json.text !== undefined ? json.text : '')
        this.type = (json.type,!== undefined ? json.type : '')
    }
}

【讨论】:

  • 可悲的是,我的声誉还不够高,无法直接评论这个问题:(
  • 本来打算用真实答案更新答案,但谢谢。
  • fromJSON 也可能没问题,但我认为它将成为构造函数。变量没有初始化。
  • 不错的尝试,但遗憾的是这仍然返回一个 jslint 警告
  • 你能试试新答案吗?
【解决方案2】:

试试这个

import BaseModel from './BaseModel'

export default class Model extends BaseModel {
constructor(){
    super()
    this.nis = this.nis || ''
    this.postal = this.postal || ''
    this.text = this.test || ''
    this.type = this.type || ''
}

【讨论】:

    猜你喜欢
    • 2019-01-26
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    相关资源
    最近更新 更多