Saturday, 6 May 2017

Node.js MongoDB updating document expects object, gets array

I'm building my first app using mongodb and node.js and I've come across a brick wall. When i try to .update() or .updateMany() my server comes back with:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): MongoError: Wrong type for 'q'. Expected a object, got a array.

Now the thing is, my code worked before and I have managed to insert an array of about 150 objects into my database. (When i first did this i used .insert() instead of .update(), but now neither works.)

Here's the exact code I'm using to first store an array of objects, and then .update() my database.

Store the array (I am using an external API for this):

  var all = [];
  $("#update").click(function(){
                          $.ajax({
                            type: "GET",
                            dataType: 'JSON',
                            url: 'some valid url',
                              success: function (result){
                                $.each(result, function(i, item) {
                                    var obj = {
                                      "name" : item['name'],
                                      "id" : item['id'],
                                      "count" : 0
                                    };
                                    all.push(obj);
                                });
                            }
                        });
  });

Send to my router.js to be processed:

    $('#sendupdate').click(function(){
    var jsonstring = JSON.stringify(all);
    console.log(jsonstring);
    $.ajax({
      type: "POST",
      dataType: "JSON",
      contentType: "application/json",
      data: jsonstring,
      url: "/s",
        success: function(response) {
          console.log('Successfully Updated');
        }
    });
  });
});

and finally here's what my router.js does to the received data:

router.post('/s', function(req, res){
  var MongoClient = mongodb.MongoClient;
  var url = 'mongodb://localhost:27017/someDB';
  MongoClient.connect(url, function(err, db){
    if(err){
      console.log('unable to connect to the server', err);
    } else {
      console.log('connected');
      var collection = db.collection('someCollection');
      var data = req.body;
      console.log('data collected');
      console.log(data);
      collection.updateMany(data, data, {upsert:true});
      }
  });
});

Now I know I could loop through the array and make a request for each object, but that feels like I'll be using too much resources, especially knowing that I will need a similar function later on for most users that enter my site.

What else could I do to update the database with so many new objects? I understand that mongodb expects to receive an object, but I don't understand why it worked before when the data received was always an array of objects.

Thank you in advance and I hope someone can shed some light on this for me.



via M. Lipski

No comments:

Post a Comment