gpt4 book ai didi

javascript - Sequelize : Object does not return after creation. 无错误。数据库中存在的对象

转载 作者:行者123 更新时间:2023-11-29 15:40:05 27 4
gpt4 key购买 nike

对象在数据库中创建,但未返回。没有错误。

User 是唯一发生这种情况的table。日志记录已开启。

用户模型:

module.exports = function (sequelize, DataTypes) {
var User = sequelize.define("User", {
email: {
type: DataTypes.STRING,
allowedNull: false,
unique: true,
validate: {
isEmail: true
}
},
username: {
type: DataTypes.STRING,
allowedNull: true,
unique: true,
validate: {
len: [4, 50]
}
},
first_name: {
type: DataTypes.STRING,
allowedNull: true,
validate: {
len: [2, 50]
}
},
last_name: {
type: DataTypes.STRING,
allowedNull: true,
validate: {
len: [2, 50]
}
},
}, {
tableName: 'User',
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
........

User 模型中没有 afterCreate Hook 。

当我调用下面的代码时,会创建一个 User 对象,但是它不会返回:

 db.User.create({email: body.email}).then((userCreated) => {
// userCreated Object is never returned, but an Object in the DB is created.
console.log("User Created " + userCreated)
}).catch((errorCreatingUser) => {
// No Error is returned either
console.log("user error " + errorCreatingUser)
})
console.log("Outside of the block ")

以下是日志:

Executing (default): SELECT `id`, `email`, `username`, `first_name`, `last_name`, `createdAt`, `updatedAt` FROM `User` AS `User` WHERE `User`.`email` = 'example@example.com' LIMIT 1;
sequelize.js:1344
Outside of the block
controller.js:478
Executing (default): INSERT INTO `User` (`id`,`email`,`createdAt`,`updatedAt`) VALUES (DEFAULT,'example@example.com','2019-09-02 16:01:29','2019-09-02 16:01:29');

您知道我做错了什么或故障排除建议吗?

更新了整个流程代码

router.post('/login', (req, res) => {
var _ = req.app.get('_')
var body = _.pick(req.body, 'username', 'email');
var db = req.app.get('db');


db.User.create({email: body.email}).then((userCreated) => {
console.log("User Created " + userCreated)
}).catch((errorCreatingUser) => {
console.log("user error " + errorCreatingUser)
})
console.log("Out of the block ")
})

最佳答案

我认为您不清楚 Node 的 promise 或异步流程,在代码中我已经按顺序注释了它将如何执行的数字

当同步模式下没有剩余进程时将执行4。

console.log("Before Create");   //<------------- 1
return db.User.create({ email: body.email}) //<------------- 2
.then((userCreated) => { //<------------- 4 ( if scuccess )
// userCreated Object is never returned, but an Object in the DB is created.
console.log("User Created " + userCreated)
}).catch((errorCreatingUser) => { //<------------- 4 ( if failed )
// No Error is returned either
console.log("user error " + errorCreatingUser)
})
console.log("Outside of the block "); //<------------- 3
<小时/>

尝试运行以下代码:

router.post('/login', (req, res) => {
var _ = req.app.get('_')
var body = _.pick(req.body, 'username', 'email');
var db = req.app.get('db');


return db.User.create({email: body.email}).then((userCreated) => { // <--------- Add return
console.log("User Created " + userCreated);
return userCreated; // <--------- Add return
}).catch((errorCreatingUser) => {
console.log("user error " + errorCreatingUser)
})
console.log("Out of the block ")
})

关于javascript - Sequelize : Object does not return after creation. 无错误。数据库中存在的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57760931/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com