为什么我不能删除mongoose模型的对象属性? [英] Why can't I delete a mongoose model's object properties?

查看:339
本文介绍了为什么我不能删除mongoose模型的对象属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户向我的API注册时,会返回一个用户对象。在返回对象之前,我删除了散列密码和salt属性。我必须使用

When a user registers with my API they are returned a user object. Before returning the object I remove the hashed password and salt properties. I have to use

user.salt = undefined;
user.pass = undefined;

因为当我尝试

delete user.salt;
delete user.pass;

对象属性仍然存在并被返回。

the object properties still exist and are returned.

为什么?

推荐答案

要使用删除,你会需要通过调用 toObject 以便您可以自由操纵它:

To use delete you would need to convert the model document into a plain JavaScript object by calling toObject so that you can freely manipulate it:

user = user.toObject();
delete user.salt;
delete user.pass;

这篇关于为什么我不能删除mongoose模型的对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆