gpt4 book ai didi

graphql - 使用种子文件将数据从 JSON 文件添加到 Prisma 数据库

转载 作者:行者123 更新时间:2023-12-01 12:08:19 26 4
gpt4 key购买 nike

我想用 JSON 文件中的一些数据为我的数据库做种子

//continent-data.json
[
{
"continent": "far east",
"imageURL": "#"
},
{
"continent": "ASIA",
"imageURL": "#"
},
]

这是我的 seed.js 文件
//seed.js
const dotenv = require('dotenv');
const { Prisma } = require('./src/generated/prisma-client');
const continentData = require('./continent-data.json');

dotenv.config();

const db = new Prisma({
secret: process.env.PRISMA_SECRET,
endpoint: process.env.PRISMA_ENDPOINT
});

const seedContinents = () => {
// adding continents to the data
Promise.all(
continentData.map(async continentItem => {
const { imageURL, continent } = continentItem;
const response = await db.createContinent({
data: {
name: continent || 'default name',
imageURL,
}
});
return response;
})
);
};

seedContinents();

当我跑 node seed.js它失败并抛出以下错误

UnhandledPromiseRejectionWarning:错误:“ContinentCreateInput!”类型的变量“$data”预期值但得到:{"data":{"name":"far Eastern","imageURL":"#","destinations":[]}}。原因:'name' 预期非空值,发现空值。

我相信我以正确的格式在那里传递正确的数据。但它说名称字段有一个空值。但错误消息本身说名称字段有一个字符串值“远东”

我还提供了 Prisma 模式的必要部分。

enter image description here

最佳答案

尝试更改向 db.createContinent 函数提供数据的方式。

 const response = await db.createContinent({
name: continent || 'default name',
imageURL,
});

只需传递数据而不是创建包装数据对象。

关于graphql - 使用种子文件将数据从 JSON 文件添加到 Prisma 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54416406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com