【问题标题】:Don't want to return the whole object, how to filter out the things that I don't want?不想返回整个对象,如何过滤掉不想要的东西?
【发布时间】:2015-01-23 00:18:13
【问题描述】:
    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",
        }
    },
}

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

【问题讨论】:

  • 如果您使用mongoosemongodb 实例中检索此数据,则可以使用select 选项过滤该字段。

标签: javascript node.js


【解决方案1】:

只需删除属性即可删除它们

delete user.user.info.password;
delete user.user.info.confirmationToken;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2022-01-13
    • 2021-04-07
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多