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');
});
和我的数据库的架构:
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,
},
});
};
我还包括了请求。request.Body
update:我更改了语法以隔离错误,它仍然显示查询。
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:我添加了数据库的图片,请确保查看那里是否有任何错误。
据我所知,这是一个基本问题,即延迟隐藏数据库错误消息。在两者之间
Error ... at Query.run
应该打印一个实际的数据库错误。由于某种原因(我也有)未显示数据库错误。
方法可以找到数据库错误是什么:可启用续集
Error ... at Query.run
之前,请查看最后一个数据库查询将这些数据库查询在数据库上直接run
如果我找到了如何正确获取续集以输出数据库错误,我将更新此答案。