【问题标题】:How to return a subcollection (or object) in json without including all attributes如何在不包含所有属性的情况下返回 json 中的子集合(或对象)
【发布时间】:2015-03-27 04:33:57
【问题描述】:

我将mongoose 用作JSON Schemanode.js。不用说,我对两者都是新手。我整天都在努力让这件事为我工作,但做不到。最后,唯一的解决方案是从这里的一些真正的好人那里获得帮助。

这是我的架构定义 -

UserName = {    
        "properties": {
            userURL: {
                    "description": "URL of this resource",
                        "type": "string"
                },
            userName : {
                    "description": "UserName",
                    "type": "string",
                    "required": true
                },
        }
}

当我调用它时,它会以以下格式返回响应 -

[
  {
    "_id": "54c5ede55c82c4bd6abee50a",
    "__v": 0,
    "properties": {
    "userURL": "http://localhost:3000/54c1d6ae441ae900151a6520",
    "userName ": "testUser"
    }
  }
]

现在我的要求是以下列格式返回响应 -

[
  {
    "_id": "54c5ede55c82c4bd6abee50a",
    "userURL": "http://localhost:3000/54c1d6ae441ae900151a6520",
    "userName ": "testUser"
  }
]

即没有版本和属性标签。我可以使用以下代码摆脱版本,但属性似乎很棘手-

 .get(function(request, response) {
        UserSchemaModel.find().select('properties.userURL properties.userName').exec (function (err, resObj) {
            if (err)
                response.send(err);
            else{           
                response.json(resObj);
            }
        });
    });

但它仍然有属性字段:( -

[
 {
    "_id": "54c5ede55c82c4bd6abee50a",
    "properties": {
    "userURL": "http://localhost:3000/54c1d6ae441ae900151a6520",
    "userName ": "testUser"
    }
 }
]

我在猫鼬中围绕select asalias name in selectpopulation 进行了一些谷歌搜索,但没有运气。

请推荐。致以最诚挚的问候。

【问题讨论】:

    标签: json node.js mongoose


    【解决方案1】:

    创建一个新对象

    response.json(
                  {
                   "_id":     resObj["_id"], 
                   "userURL": resObj["properties"]["userUrl"], 
                   "userName": resObj["properties"]["userName"]
                  }
               );
    

    更新:由于 resObj 是一个数组(根据您的评论),您可以使用 Array.prototype.map() 将它们转换为正确的格式,如下所示:

    response.json( resObj.map(function(o){
                     return {
                              "_id":      o["_id"], 
                              "userURL":  o["properties"]["userUrl"], 
                              "userName": o["properties"]["userName"]
                            };
                        }) 
                     );
    

    这将返回转换后的对象列表,然后将其传递给response.json() 方法。

    【讨论】:

    • 很遗憾,它没有用。 "userURL": resObj["properties"]["userURL"] ^ TypeError: Cannot read property 'userURL' of undefined 我之前尝试过 resObj.properties.userURL,但同样的错误。
    • 伙计,你给了我一个线索,我领先一步。 resObj 是 json 对象的集合,因此 resObj[0] 似乎正在工作。关于如何遍历所有对象并使其工作的任何指针?因为我们只能发送一次response 对象。
    • 太棒了!万分感谢 !!奇迹般有效 !!你想给我指出一些好的资源,比如Array.prototype.map()。这确实是一个很大的帮助。
    • 如果您对该主题感兴趣,您可以阅读有关higher order functions 作为通用编程范例的文章。这篇文章使用javascript解释了一些事情:eloquentjavascript.net/05_higher_order.html
    猜你喜欢
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多