'Error at Query.run when using postman and sequelize
I am receiving this console error when trying to run my postman POST to see if it transfers over into my database.
Server started at http://localhost:5000
Error
at Query.run (C:\Users\donal\videos\Web-Developement\videojs\vc-be\server\node_modules\sequelize\lib\dialects\postgres\query.js:50:25)
at C:\Users\donal\videos\Web-Developement\videojs\vc-be\server\node_modules\sequelize\lib\sequelize.js:313:28
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async PostgresQueryInterface.insert (C:\Users\donal\videos\Web-Developement\videojs\vc-be\server\node_modules\sequelize\lib\dialects\abstract\query-interface.js:297:21)
at async model.save (C:\Users\donal\videos\Web-Developement\videojs\vc-be\server\node_modules\sequelize\lib\model.js:2424:35)
at async Function.create (C:\Users\donal\videos\Web-Developement\videojs\vc-be\server\node_modules\sequelize\lib\model.js:1336:12)
at async C:\Users\donal\videos\Web-Developement\videojs\vc-be\server\controllers\auth.js:12:18
Here is my code below:
const { Op } = require("sequelize");
const { User } = require('../sequelize');
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const asyncHandler = require('../middlewares/asyncHandler');
// exports.signup = asyncHandler(async (req, res, next) => {
// console.log('this function works');
// });
exports.signup = asyncHandler(async (req, res, next) => {
const user = await User.create(req.body);
await user.save();
res.status(200).json({ success: true, data: token });
console.log('this function works');
});
and my schema for my database:
const { Sequelize } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
return sequelize.define("User", {
id: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
defaultValue: Sequelize.UUIDV4,
},
firstname: {
type: DataTypes.STRING,
allowNull: false,
},
lastname: {
type: DataTypes.STRING,
allowNull: false,
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true,
},
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
min: 6,
},
},
avatar: {
type: DataTypes.STRING,
defaultValue:
"https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/b0b4c759-ad9c-4425-a9f4-ab89e2fd9837/de8cefl-35c0bc59-59b9-42ab-b19f-5c73828bb78e.png?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7InBhdGgiOiJcL2ZcL2IwYjRjNzU5LWFkOWMtNDQyNS1hOWY0LWFiODllMmZkOTgzN1wvZGU4Y2VmbC0zNWMwYmM1OS01OWI5LTQyYWItYjE5Zi01YzczODI4YmI3OGUucG5nIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXX0.81ixeN9b4cfDmfBlskK9CUyAMDtRhYNU7lfwTI8WI5Q",
},
cover: {
type: DataTypes.STRING,
defaultValue:
"https://pbs.twimg.com/profile_images/1410507368622919685/AU_Zqm1J_400x400.jpg",
},
channelDescription: {
type: DataTypes.STRING,
},
isAdmin: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
});
};
I am new to sequelize and axios. Still trying to figure out why I am getting this error. If anyone can give me some pointers, I'd appreciate it.
I have also included the request.body, to see if there is something wrong with it. Request.Body
UPDATE: I have changed the syntax to isolate the error, it still shows query.run, it now returns the error 500 whenever I use this specific syntax:
exports.signup = async (req, res, next) => {
try {
const { username, email, password, firstname, lastname } = req.body;
//create user document and save in database
const user = await User.create({
username,
email,
password,
firstname,
lastname,
});
await user.save();
res.status(201).send(
'successfully sent!'
)
} catch (err) {
return res.status(500).send('Error occured. Please try again');
}
};
UPDATE: I have added a picture of the databases, making sure to see if there's any errors there.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
