typeorm 保存列表为字符串
typeorm saving list as string
我有一个 flutter 前端,它在 ['url'、'url'、'url'] 之类的列表中发送 url 个字符串,但在 Postgres 中它是 saved/on检索显示为单个字符串,如 'url,url,url'.
这是有问题的列:
@Column({
type: "simple-array",
default: [],
})
postMedia!: string[];
这是保存 post 的存储库方法:
async addPost(req: Request, res: Response) {
let { email } = req.params;
let postText = req.body.postText;
let postMedia: string[] = req.body.postMedia;
let accountRepo = getCustomRepository(AccountRepository);
let account = await accountRepo.findOne({
email: email,
});
try {
let post = new PostEntity();
post.postText = postText;
post.postMedia = postMedia;
post.account = account!;
await post.save();
return res.send({
message: "Post Added",
added: true,
});
} catch (error) {
console.log(error);
return res.send({
message: "Something went wrong",
added: false,
});
}
}
问题出在哪里?为什么即使我将 postMedia 变量键入为 string[]
,列表仍未保存为数组?请帮忙
这对我有用
@Column("text", { array: true })
postMedia!: string[];
如果这能解决您的问题,请告诉我
我有一个 flutter 前端,它在 ['url'、'url'、'url'] 之类的列表中发送 url 个字符串,但在 Postgres 中它是 saved/on检索显示为单个字符串,如 'url,url,url'.
这是有问题的列:
@Column({
type: "simple-array",
default: [],
})
postMedia!: string[];
这是保存 post 的存储库方法:
async addPost(req: Request, res: Response) {
let { email } = req.params;
let postText = req.body.postText;
let postMedia: string[] = req.body.postMedia;
let accountRepo = getCustomRepository(AccountRepository);
let account = await accountRepo.findOne({
email: email,
});
try {
let post = new PostEntity();
post.postText = postText;
post.postMedia = postMedia;
post.account = account!;
await post.save();
return res.send({
message: "Post Added",
added: true,
});
} catch (error) {
console.log(error);
return res.send({
message: "Something went wrong",
added: false,
});
}
}
问题出在哪里?为什么即使我将 postMedia 变量键入为 string[]
,列表仍未保存为数组?请帮忙
这对我有用
@Column("text", { array: true })
postMedia!: string[];
如果这能解决您的问题,请告诉我