expressでAPIを構築しています。
Schemaは UserとPostのシンプルな構成です。
Postmanを使った結果を以下に示します。
ORMはsequelizeです。
原因と解決方法がわかる方がいらっしゃいましたらお力添えをお願いしたいです。
よろしくお願いします。
結果
新規投稿
// 結果
{
"id": 1,
"userId": 1,
"content": "1回目の投稿です",
"updatedAt": "2022-09-09T13:40:08.247Z",
"createdAt": "2022-09-09T13:40:08.247Z"
}
先の投稿を取得
下記の結果のUserIdが重複してしまう。 UserIdは1つで良いのでなんとかしたい
// 結果
{
"id": 1,
"userId": 1,
"content": "1回目の投稿です",
"createdAt": "2022-09-09T13:40:08.000Z",
"updatedAt": "2022-09-09T13:40:08.000Z",
"UserId": 1
}
ソースコード
user
のmigrationファイル
"use strict";
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable("Users", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
name: {
allowNull: false,
type: Sequelize.STRING,
},
email: {
allowNull: false,
type: Sequelize.STRING,
},
password: {
allowNull: false,
type: Sequelize.STRING,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable("Users");
},
};
post
のmigrationファイル
"use strict";
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable("Posts", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
userId: {
allowNull: false,
type: Sequelize.INTEGER,
},
content: {
allowNull: false,
type: Sequelize.STRING,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable("Posts");
},
};
post model
"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class Post extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
Post.belongsTo(models.User);
}
}
Post.init(
{
userId: DataTypes.INTEGER,
content: {
type: DataTypes.STRING,
allowNull: false,
validate: { notNull: { msg: "内容は必ず入力してください" } },
},
},
{
sequelize,
modelName: "Post",
}
);
return Post;
};
postのAPI
const express = require("express");
const router = express.Router();
const { Post } = require("../models");
// 全ての投稿を取得
router.get("/", async (req, res) => {
try {
const posts = await Post.findAll();
return res.status(200).json(posts);
} catch (err) {
return res.status(500).json(err);
}
});
// 特定の投稿を取得
router.get("/:id", async (req, res) => {
try {
const post = await Post.findByPk(req.params.id);
return res.status(200).json(post);
} catch (err) {
return res.status(500).json(err);
}
});
// 新しい投稿を作成
router.post("/create", async (req, res) => {
try {
const post = await Post.create({
userId: req.body.userId,
content: req.body.content,
});
return res.status(200).json(post);
} catch (err) {
return res.status(500).json(err);
}
});
// 投稿を更新
router.put("/:id", async (req, res) => {
try {
} catch (err) {
return res.status(500).json(err);
}
});
// 投稿を削除
router.delete("/:id", async (req, res) => {
try {
} catch (err) {
return res.status(500).json(err);
}
});
module.exports = router;