Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从JSON文件创建_Node.js_Sequelize.js - Fatal编程技术网

Node.js 从JSON文件创建

Node.js 从JSON文件创建,node.js,sequelize.js,Node.js,Sequelize.js,我想创建包含要插入的记录的JSON文件,然后使用model.bulkCreate一次插入所有记录,但当我读取该文件时,我得到“TypeError:records.map不是函数” database.js fs.readFile(__dirname + "/databaseTables/consoleAndPlatform.json", function(err, data){ ConsoleAndPlatform.bulkCreate(data.toString()) }) con

我想创建包含要插入的记录的JSON文件,然后使用model.bulkCreate一次插入所有记录,但当我读取该文件时,我得到“TypeError:records.map不是函数”

database.js

fs.readFile(__dirname + "/databaseTables/consoleAndPlatform.json", function(err, data){
    ConsoleAndPlatform.bulkCreate(data.toString())
  })
consoleAndPlatform.json

[{console: "XBOX",
platform: "XBOX 360",
name: "Xbox 360"},{
console: "XBOX",
platform: "XBOX ONE",
name: "Xbox One"},{
console: "XBOX",
platform: "XBOX LIVE",
name: "Xbox Live"},{
console: "PS",
platform: "PS4",
name: "Playstation 4"}]

谢谢你的帮助

好的,我找到了解决方案,也许对某人会有帮助。首先,JSON是错误的,应该是:

[
{
    "console": "XBOX",
    "platform": "XBOX 360",
    "name": "Xbox 360"
},
{
    "console": "XBOX",
    "platform": "XBOX ONE",
    "name": "Xbox One"
},
{
    "console": "XBOX",
    "platform": "XBOX LIVE",
    "name": "Xbox Live"
}]
其次,您需要解析字符串结果:

ConsoleAndPlatform.bulkCreate(JSON.parse(data.toString()))