从JSON文件

时间:2017-09-10 13:51:41

标签: node.js sequelize.js

我想创建带有要插入的记录的JSON文件,然后使用model.bulkCreate一次插入所有记录但是当我读取文件时我得到&#34; TypeError:records.map不是函数&#34; < / p>

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"}]

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

好的,我发现解决方案可能对某人有帮助。首先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"
}]

第二,你需要JSON.parse字符串结果:

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