2

I'm very fresh into both node.js and mongodb but I do really like it thus far. I'm trying to make an api-call and insert the data recieved into mongodb. Using cloud9 IDE btw.

On SO now I've only found posts about importing json files such as this one Insert json file into mongodb

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 = data;
          db.collection("dabas").insertMany(myobj, function(err, res) {
            if (err) throw err;
            console.log("Number of documents inserted: " + res.insertedCount);
            db.close();
          });
        });
     }
});

A sample from the json file looks like this:

// 20171013085454
// http://api.dabas.com/DABASService/V2/article/GTIN/07310100774460/json?apikey=2d2fbadc-dbe1-420f-a777-65912d65e388

{
  "Artikelbeskrivning": null,
  "Artikelegenskap": null,
  "Produktkod": "103511784459 / Kolonial/Speceri -- Kakao/Chokladdryck -- Kakaopulver -- Kakaopulver",
  "OvrigObligatoriskMarkning": null,
  "MaximalaAntaletMinstaEnheterIforpackningen": 0,
  "Hyllkantstext": "Kakaopulver 20-22",
  "Storlek": "1kg",
  "TullstatistisktNummer": null,

  "Varningsetiketter": [

  ],
  "Sasongskoder": [

  ],
  "Produktklasser": [

  ],
  "MaskinellMarkningar": [
    {
      "MaskinellMarkning": null,
      "TypAvMarkning": null,
      "Databarartyp": "EAN UPC"
    }
  ]}

When running I get this error message:

/home/ubuntu/workspace/Projects/Mathubben/node_modules/mongodb/lib/mongo_client.js:433
          throw err
          ^
MongoError: docs parameter must be an array of documents
    at Function.MongoError.create (/home/ubuntu/workspace/Projects/Mathubben/node_modules/mongodb-core/lib/error.js:31:11)
    at Collection.insertMany (/home/ubuntu/workspace/Projects/Mathubben/node_modules/mongodb/lib/collection.js:501:32)
    at /home/ubuntu/workspace/Projects/Mathubben/app.js:26:38
    at connectCallback (/home/ubuntu/workspace/Projects/Mathubben/node_modules/mongodb/lib/mongo_client.js:515:5)
    at /home/ubuntu/workspace/Projects/Mathubben/node_modules/mongodb/lib/mongo_client.js:430:11
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)
Ola Karlsson
  • 141
  • 1
  • 2
  • 10
  • 1
    *"docs parameter must be an array of documents"* Seems pretty descriptive. The result of JSON.parse is not an array. Wrap it in an array or inspect it and work out why it is not an array if you are expecting it to be one. Based on the response in your question you should either do: `db.collection("dabas").insertMany([myobj], function(err, res) {` to wrap in an array, or simply just use `.insert()` which expects a "single" document: `db.collection("dabas").insert(myobj, function(err, res) {` – Neil Lunn Oct 13 '17 at 07:01

3 Answers3

2

It was the insertMany which was the issue

 db.collection("dabas").insertMany(myobj, function(err, res) {

when using

 db.collection("dabas").insert(myobj, function(err, res) {

It worked properly

Ola Karlsson
  • 141
  • 1
  • 2
  • 10
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/

Anushka Ahir
  • 136
  • 7
0

The insertMany method accepts an array of documents

insertMany([ <doc 1> , <doc 2>, ... ],{
  writeConcern: <document>,
  ordered: <boolean>
  }
)

You are passing an object.

Solution 1: Assuming that you will receive a singe object for the body parameter, you can modify your code as below:

request(url, function(error, response, body){
 if(!error && response.statusCode == 200){
    var data = []
    var data.push(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 = data;
      db.collection("dabas").insertMany(myobj, function(err, res) {
        if (err) throw err;
        console.log("Number of documents inserted: " + res.insertedCount);
        db.close();
      });
    });
 }
});

Solution 2: You need to modify code to receive Json data in an array.

ritesh sangani
  • 280
  • 3
  • 8