这是一个从JSON对象中删除敏感信息返回给客户端的功能。传递给函数的数据可能是JSON对象或JSON对象数组。为什么这个功能不起作用?删除不支持Javascript的操作符
我知道有这个问题的其他解决方案,但是这很讨厌我的大脑。
我已经在这个函数记录大量的信息,并且即使JavaScript是异步的功能顺序,他们应该运行 - 递归是最后return语句被击中之前完成。
现在的问题是,即使一切似乎都正常并且delete
运算符返回true
,当函数最终返回时,被删除的属性仍然存在。
例data
,其从MongoDB中被获取:
[
{
'id': '1',
'name': 'John',
'password': 'test123',
'emailAddress': '[email protected]',
'emailAddressVerificationCode': 'A897D'
},
{
'id': '2',
'name': 'Andrew',
'password': 'test123',
'emailAddress': '[email protected]',
'emailAddressVerificationCode': '90H8D'
},
{
'id': '3',
'name': 'Matthew',
'password': 'test123',
'emailAddress': '[email protected]',
'emailAddressVerificationCode': '56C7C'
}
]
任何想法,将不胜感激。
UserService.cleanJSON = (data) => {
if (Array.isArray(data)) {
for (let i = 0; i < data.length; i++){
data[i] = UserService.cleanJSON(data[i]);
}
} else {
if (data.password) delete data.password;
if (data.emailAddressVerficationCode) delete data.emailAddressVerficationCode;
if (data.mobileNumberVerificationCode) delete data.mobileNumberVerificationCode;
if (data.accountType) delete data.accountType;
}
return data;
};
你能否提供一个初始化'data'的例子,它不起作用? –
此外,'可能'与您的问题无关,但可能不需要从函数中返回'data',因为它正在编辑通过引用传入的原始对象。 –
此功能将按预期工作。问题在别的地方。 – Schlaus