不想要归还整个对象,如何过滤掉我不想要的东西?

时间:2015-01-23 00:18:14

标签: javascript node.js

    return res.send({"user": user});

返回的用户对象是这样的(见下文),但我想隐藏密码,confirmationToken以及__v字段。

{
    "user": {
        "_id": "566786",
        "detail": {
            "lastUpdate": "2015-01-22T22:06:22.951Z",
            "registrationDate": "2015-01-22T02:15:07.379Z",
            "firstName": "Testing",
            "lastName": "Testing",
        },
        "info": {
            "confirmationToken": "3dc917fbfd6f47e",
            "password": "$2a$08$4ugeIPC.5nCMQiyutL11i",
            "email": "testing@abcdefgxxxxxx.com",
        }
    },
}

我希望它是这样的:

{
    "user": {
        "_id": "566786",
        "detail": {
            "lastUpdate": "2015-01-22T22:06:22.951Z",
            "registrationDate": "2015-01-22T02:15:07.379Z",
            "firstName": "Testing",
            "lastName": "Testing",
        },
        "info": {
            "email": "testing@abcdefgxxxxxx.com",
        }
    },
}

我应该如何过滤掉那些我不想要的字段?

1 个答案:

答案 0 :(得分:1)

只需删除属性

即可删除
delete user.user.info.password;
delete user.user.info.confirmationToken;

return res.send({"user": user});