Sequelize:不要在创建时返回密码

编程入门 行业动态 更新时间:2024-10-23 04:46:16
本文介绍了Sequelize:不要在创建时返回密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我使用 Sequelize 创建我的用户的新实例时,我会从数据库中获取完整的对象作为对 Create 的响应.我试图阻止 Sequelize 在创建时返回存储在我的用户表中的散列密码.

When I create a new instance of my User with Sequelize, I get the full object returned from the database as a response to the Create. I'm trying to prevent Sequelize from returning the hashed password stored in my User table on create.

exports.create = (payload, err, success) => { db.user.create(payload).then(success).catch(err); console.log('Created new user record.'); };

我尝试使用 exclude 属性,但返回完整对象.

I tried using the exclude property, but the full object returns.

exports.create = (payload, err, success) => { db.user.create(payload, { attributes: { exclude: ['password'] } }).then(success).catch(err); console.log('Created new user record.'); };

我可以在我的 find 和 findAll 路由的属性中使用 exclude 属性,如下例所示,但我无法让它与我的 create 一起使用.

I am able to use the exclude property in attributes on my find and findAll routes like the example below, but I haven't been able to get it to work with my create.

exports.find = (payload, err, success) => { db.user.find({ where: { id: payload.id, }, attributes: { exclude: ['password'] } }).then(success).catch(err); console.log('Retrieved user record with id: ' + payload.id); };

推荐答案

您可以尝试使用 toJSON 排除属性的实例方法:

You can try using toJSON instance method to exclude attributes:

型号:

instanceMethods: { toJSON: function () { const userObj = Object.assign({}, this.dataValues); delete userObj.password; return userObj } }

路由文件:

user.create(request.body, (err) => { res.status(500).json(err); console.log('Error creating new user.'); }, (data) => { res.status(200).json(data.toJSON()); console.log('User created.'); });

更多推荐

Sequelize:不要在创建时返回密码

本文发布于:2023-10-29 03:38:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538614.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:密码   不要在   Sequelize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!