Reputation: 46
I am using Koa router, with Sequelize. And I have used Sequelize init, where the model is created as follows:
module.exports = (sequelize, DataTypes) => {
class User extends Model {
static associate(models) {
// Associations...
}
}
User.init(
{
email: {
type: DataTypes.STRING,
allowNull: false,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
sequelize,
modelName: 'User',
timestamps: false,
}
);
return User;
};
And the koa-router has:
router.get('/:id', UserController.get);
Middleware has:
const db = require('../../../models/index');
module.exports = () => async (ctx, next) => {
try {
ctx.db = db;
next();
} catch (err) {
console.error(err);
ctx.status = 500;
}
};
Now, I have the following code:
UserController.get = async (ctx) => {
try {
const user = await ctx.db.User.findOne({
where: { id: ctx.params.id });
if (user) {
ctx.body = user;
ctx.status = 200;
} else {
ctx.status = 404;
}
} catch (error) {
ctx.status = 500;
}
};
When I was stepping through debugger, I am seeing when the debugger moves to if (user) {
line the response from this GET request returns as 404, even though the function UserController.get
is not complete yet.
Any ideas why this could be?
Upvotes: 1
Views: 523
Reputation: 160
const user = await ctx.db.User.findOne({
where: { id: ctx.params.id }
});
You forgot a closing bracket
Upvotes: 0