6

I am trying to get JSON from an API and store it into a MongoDB database. Obviously, it doesn't work. My app seems to hang around the point where I try to save the data to the database. Please advise what to do.

Here's my code:

var express = require('express');
var router = express.Router();
var http = require('http');
var mongo = require('mongoskin');
var db = mongo.db("mongodb://localhost:27017/zak", {native_parser : true});


/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});


var site = 'http://www.vsechnyzakazky.cz/api/v1/zakazka/?format=json&limit=2';


function getData(cb) {

    http.get(site, function(res) {
        // explicitly treat incoming data as utf8 (avoids issues with multi-byte chars)
        res.setEncoding('utf8');

        // incrementally capture the incoming response body
        var body = '';
        res.on('data', function(d) {
            body += d;
        });

        // do whatever we want with the response once it's done
        res.on('end', function() {
            try {
                var parsed = JSON.parse(body);
            } catch (err) {
                console.error('Unable to parse response as JSON', err);
                return cb(err);
            }

            // pass the relevant data back to the callback
            cb(
                parsed.objects
               );
        });
    }).on('error', function(err) {
        // handle errors with the request itself
        console.error('Error with the request:', err.message);
        cb(err);
    });

}

function writeData (data, allGood){     

// couple of visual checks if all looking good before writing to db
console.log('writing');
console.log(typeof data);
console.log(data);

db.collection('zakazky').save(data, function(error, record){
if (error) throw error;
console.log("data saved");

});
}

function allGood(){console.log('all done');}

getData(writeData);

// ---------------------
module.exports = router;
2
  • Does the data appear in the database? Commented Mar 18, 2015 at 11:57
  • No. I actually inserted manually some data to test if I setup the DB connection correctly. Upon using 'db.zakazky.find()' I see that test data, but not what I'm trying to save Commented Mar 18, 2015 at 13:35

1 Answer 1

4

You are calling the save() instead of insert(). Change this part and it will work:

// this should call insert, not save
db.collection('zakazky').insert(data, function(error, record){
    if (error) throw error;
    console.log("data saved");
});
Sign up to request clarification or add additional context in comments.

2 Comments

save should work fine here. However, some MongoDB drivers may expect second .save() parameter to be an options object.
Thank you @victorkohl. Insert() indeed worked. I am surprised and wondering why, because that's what I've been trying to work with initially and only as a last resort switched to save(). Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.