[Vuejs]-How put JSON data to MongoDB?

0๐Ÿ‘

โœ…

//modify your code like this
var url = "API CALL HERE"

request(url, function(error, response, body){

 if(!error && response.statusCode == 200){
    var data = JSON.parse(body);
    res.send(data);

    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/mydb";

    MongoClient.connect(url, function(err, db) {
      if (err) throw err;
      var myobj = [];
      myobj.push(data);
      db.collection("dabas").insertMany(myobj, function(err, res) {
        if (err) throw err;
        console.log("Number of documents inserted: " + res.insertedCount);
        db.close();
      });
    });
 }
});

//your problem is you passing obj not array
//see below link for your reference

https://docs.mongodb.com/manual/reference/method/db.collection.insertMany/

Leave a comment