async create(
userId,
title,
content,
subjectId,
type,
categoryId,
coverImageUrl,
tags,
published
) {
const ctx = this.ctx;
let transaction;
try {
transaction = await ctx.model.transaction();
const post = await ctx.model.Post.create(
{
author_id: userId,
title,
content,
type,
category_id: categoryId,
cover_image_url: coverImageUrl,
subject_id: subjectId,
published,
created_at: new Date(),
updated_at: null,
deleted_at: null,
length: 2,
render_content: "",
subtitle: "",
leading_text: ""
},
{ transaction }
);
const postId = post && post.getDataValue("id");
if (!postId) {
throw new Error();
}
const tagIds = [];
for (const tagName of tags) {
const result = await ctx.model.Tag.findOrCreate({
where: { name: tagName },
transaction
});
tagIds.push(result[0].dataValues["id"]);
}
for (const tagId of tagIds) {
await ctx.model.PostTag.create(
{ post_id: postId, tag_id: tagId },
{ transaction }
);
}
await transaction.commit();
return { success: true, data: { postId } };
} catch (e) {
console.log(e);
await transaction.rollback();
return { success: false, message: "服务器异常" };
}
}
没有提示错误,接口可以返回 postId,但查数据库后,数据根本没插进去,请问是什么原因呢?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.