需要JSON文件时JSON输入意外结束

时间:2019-01-09 19:17:07

标签: javascript express ecmascript-6

使用fs.writeFile时,我创建一个类似于以下内容的JSON文件:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    //If transactionPage is set to -1, it's because we've reached the end of the transactions
    if transactionPage != -1 && indexPath.row == (tableData.rows(inSection: indexPath.section).count) - 1 {
        loadMoreTransactions()
    }

    if arrIndexPath.contains(indexPath) == false {
        cell.alpha = 0

        //Slide from bottom
        let transform = CATransform3DTranslate(CATransform3DIdentity, 0, 200, 
        0)

        cell.layer.transform = transform

        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 1, 
            initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            cell.alpha = 1
            cell.layer.transform = CATransform3DIdentity
        })

        arrIndexPath.append(indexPath)
    }
}

除了在我需要相同文件之后,我收到错误消息:

[{"schoolname":"Scottsdale","StudentFirst":"john","StudentLast":"smith","grade":"2","email":"john@smith.com","ParentLast":"Smith","ParentFirst":"John"}]

我之前也做过同样的事情,但是使用了更大的数据,它似乎工作正常。

Unexpected end in JSON input

预期结果应返回上面的JSON,这样我就可以得到文件的副本并进行遍历。

1 个答案:

答案 0 :(得分:1)

该错误是由于需要一个空JSON文件而导致的。 JSON文件为空,因为set(title) == set(title1) 是异步的,并且您未正确使用fs.writeFile

  1. 您需要在异步函数中使用await
  2. 您只能将await用于承诺,不能用于回调

选项1:使用await

fs.writeFileSync

选项2:将fs.writeFileSync( `./${fileName}.json`, JSON.stringify(result.recordsets[0]) ) const file = require(`./${fileName}.json`) 包裹在诺言中

fs.writeFile