gpt4 book ai didi

node.js - sails JS : can not create Model in beforeCreate

转载 作者:搜寻专家 更新时间:2023-11-01 00:39:50 25 4
gpt4 key购买 nike

我使用 SailsJS (v0.12.3),我想在创建配置文件之前创建一个用户。我的问题是用户已成功创建,但 UserProfile 没有。

这是我的 UserProfile 模型:

beforeCreate: function (userProfile, cb) {
// Creating user login
console.log("Creating user login: ", userProfile);
User.create({
username: "abc",
password: "1234567890"
}).then(function (user) {
console.log("Creating user done: ", user)
userProfile.appUser = user.id;
userProfile.code = user.username;
cb(null, userProfile);
}).catch(function (err) {
console.log("ABC", err);
cb(err);
});
}

控制台输出:

Creating user login:  { firstName: 'Vu',
lastName: 'Bao',
gender: 1,
phoneNumber: '0987654321',
id: '29b1b37b-ca95-4fd0-8cf8-e35a25af4616',
code: '1469529082587',
isVerified: false,
totalVisit: 0 }

我可以看到打印了消息 Creating user login ...,但是我看不到日志消息 Creating user done ...ABC 错误。

结果:在数据库中我创建了 useruserProfile 没有。

如何在正确创建 UserProfile 之前创建用户模型?

谢谢!

最佳答案

一段时间后阅读模型操作 Lifecycle callbacks .我发现我的代码没有回调我在 User 模型中定义的函数 afterCreate。所以,我确实删除了 afterCreate 函数,然后它就可以工作了。

// ./api/models/User.js
afterCreate: function (user, cb) {
// i commented a lots in this function, but did not callback cb
// then i remove this method because I did not need it anymore
// ...
}

感谢阅读。希望这对其他人有帮助!!

关于node.js - sails JS : can not create Model in beforeCreate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38587679/

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