当前位置: 首页 > 知识库问答 >
问题:

无法向Mongoose查询返回的对象添加新属性[重复]

宗建章
2023-03-14

我编码一个API与Node.js,MongoDB和快递。我似乎无法向我正在迭代的位置对象添加新属性。

我完全不理解我的代码的问题。loc是一个普通的对象,它应该可以工作。我说错了吗?

// **********************************
// GET Locations
// **********************************
// Create endpoint /api/locations for GET

exports.getLocations = function(req, res) {

    // Use the Location model to find all locations
    // of a particular user

    Location.find({}, function(err, locations) {
        if (err)
            res.send(err);
        var counter = 0;
        var l = locations.length;

        //we create a closure to access the current location object
        var closure = function(location) {

             //we are returning the callback
             return function(err, user) {
                if (err)
                    res.send(err);
                counter++;
                console.log("The location object: \n"+location+"\n\n");

                console.log("The value we want to add to the object: \n"+user.username+"\n\n");

                //THAT DOESN'T WORK
                location.username = user.username;

                console.log("The updated object: \n"+location+"\n\n");

                if(counter === l) res.json(locations);
            };
        };

        //We respond with the locations
        for (var i = 0; i < l; i++) {

            //we call our function
            User.findById(locations[i].userId, "username", closure(locations[i]));
        }
    });
};

这是我在控制台中得到的输出。。。这太奇怪了。

The location object:
{ _id: 54c65c665ff13962b6a367a1,
  userId: '54c659ba8ac00324b617f3f9',
  message: 'Big party here tonight!!',
  longitude: 45.5,
  latitude: 73.5667,
  __v: 0 }


The value we want to add to the object:
test123


The updated object:
{ _id: 54c65c665ff13962b6a367a1,
  userId: '54c659ba8ac00324b617f3f9',
  message: 'Big party here tonight!!',
  longitude: 45.5,
  latitude: 73.5667,
  __v: 0 }

共有1个答案

马野
2023-03-14

在我们的例子中,您有MongooseDocumentnotplainJS对象。为了获得普通的JS对象,您应该使用。像这样精益

Location.find({}).lean().exec(function(err, locations) {
    ...
});

 类似资料:
  • 我想向云Firestore中已存在的对象添加新的属性 如您所见,我只是向一个对象添加了新属性。 现在这段代码每次都用旧属性覆盖新属性。

  • 问题内容: 这件事困扰了我一段时间。我为什么不能做: …虽然我可以执行以下操作? 这是什么规则?您能给我介绍一下吗? 问题答案: 您可以向具有的任何对象添加属性。 例如,没有它。 字符串和其他简单的内置对象也没有它。 使用类也没有它。 除非前面的声明适用,否则用定义的类都具有它。 如果使用/的对象没有/ ,通常是为了节省空间。例如,如果有一个命令,那就太过分了- 想象一下一个非常短的字符串的膨胀程

  • 问题内容: 因此,我使用猫鼬已经有一段时间了,我发现确实发生了一些很奇怪的事情。如果有人能启发我,那就太好了。 问题是,当使用猫鼬的.find()方法时,作为响应获得的对象充满了我不知道它来自何处的属性(我猜它们是内置属性,但无论如何),我只想通过I.select()属性进行迭代。得到它了?没有?好…解释得更好: 我声明了架构和模型: 然后,我想查找一个名称为John的文档,并检索除“名称”字段以

  • 我正在从Mongoose查询返回一个文档对象数组。数组中填充了预期的结果。 我试图添加一个新的属性到某些对象,只有在满足一个标准。我已经测试了这个元素,条件逻辑也按预期执行。我在上面的数组和另一个更大的数组中循环查找匹配的id。当找到匹配时,我需要添加一个属性,例如: 指向由fint()查询返回的数组中的对象。所以我应该以: 问题是,无论我尝试什么,都无法将新属性添加到数组中的特定对象上。 我在这

  • 我想要得到一个这样的对象: 通过这样编码: 但我总是得到以下几点:

  • 问题内容: 我要返回一个Mongoose文档,并希望在发送之前向其中添加一些元数据。但是,我无法添加任何属性,我不确定为什么。我检查了它是否可以使用Object.isExtensible(doc)扩展。 可能是什么问题? 问题答案: 啊..我的对象是一个Mongoose文档,不允许添加属性。解决方案是将返回的文档转换为普通对象,或者在查询中调用lean()。