- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我使用 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
错误。
结果:在数据库中我创建了 user
但 userProfile
没有。
如何在正确创建 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/
更新我的 sails (0.10-rc5) 后, 我在 beforeCreate 函数中遇到问题: beforeCreate : function(values, next){ con
我正在使用 beforeCreate在保存到数据库之前加密密码。 当我做: const user = await User.create({name, email, password}); res.j
我有 Customer 结构 type Customer struct { Model Email string `json:"email,om
我不确定为什么数据没有从 this.resp.memo 渲染。我认为它应该加载,因为它保存数据,然后渲染 veiw 和所有数据 {{
我正在使用 sailsjs 版本 0.11.0。我想在创建新用户时必须先加密他们的密码,然后再将其存储到数据库中。为此,我使用了 bcrypt 库。我在 User.js 模型中添加了一个方法 befo
在我的用户模型中,我定义了一个生命周期 Hook ,用于使用 bycrypt 加密密码。 beforeCreate: function(acc, next) { const salt = bc
我正在尝试在 nodejs 中创建一个带有 sequelize 的用户模型,但似乎我的 beforeCreate 实际上并没有被调用。我查看了互联网上的文档和多个示例,但无法正常工作。 型号代码如下:
在我的 sails.js 应用程序模型中,我想从生命周期回调发送“错误请求”错误代码 400。这是我的 beforeCreate 函数: beforeCreate: function (values,
我正在尝试验证外键是否存在。如果外键有效,我只想创建一个对象。 我正在 beforeCreate 中查询对象,但是当我调用带有错误消息的回调时,错误未得到处理(回溯)并返回 500。 我想优雅地处理错
我有一个带有非常基本模型的 blank sails 0.9.8 应用程序: module.exports = { attributes: { nickname: 'string',
我不明白 Vuejs 中 beforeCreate 的用途。如果它只在你的组件被添加到 DOM 之前执行操作,那么我只在使用 vue 实例之前添加一些行代码,如下所示: var d
Nuxt 和 SSR 的新手。我想要做的很简单:我试图在页面加载之前触发一个函数,该函数将对我的 firestore 数据进行 SSR。如果我用@onclick 添加一个按钮,它工作正常。但是当我尝试
我在我的 sequelize 模型中写了一个 BeforeCreate 钩子(Hook)。当我点击创建用户路由时,它说 user.user_id 不能为空,甚至在创建钩子(Hook)函数未执行之前。我
我使用直接方法( Sequelize 文档中的方法 3)定义了我的 beforeCreate 钩子(Hook),如下所示: module.exports = (sequelize, datatypes
关于Firebase docs他们提到了 4 种类型的触发器: 创建时 删除时 更新时 写入时 有没有办法在触发“child_added”监听器之前监听云函数中添加的行并修改添加行的字段?有没有办法实
其实我觉得这是不可能的。但无论如何都会问。我正在尝试从数据库中激活一些组件。所以我得到了组件“方法”中一些事件组件的数组列表。因为我无法访问文件开头的列表变量,所以我想在数组列表变量准备就绪时在 be
我需要获取一些 header “用户代理”以及 IP 地址,如何在 beforeCreate 中执行此操作? 最佳答案 req 对象在水线中不可用。所以下一个问题是您是否使用蓝图。 如果是,那么您可以
我正在尝试在创建和保存密码之前对其进行哈希处理。但是,当我将 Hook 定义如下时,我注意到日志使用原始密码创建了一个 guest 实例,然后立即更新它。我不知道为什么。 // define mode
我有一个用户模型,我只是尝试通过返回 err 来测试 beforeCreate,但是当我执行 POST/user 时,它会创建模型很好,不执行 beforeCreate 方法。我在这里做错了什么?这是
如果我尝试使用在 beforeCreate() 方法中定义的 date_created 字段保存模型,它不会保存它: class TestEntity extends Phalcon\Mvc\Mode
我是一名优秀的程序员,十分优秀!