codeDragon
codeDragon

Reputation: 565

Data insertion with Nodejs to MongoDb does not work

I am trying to insert data with Nodejs, and there is something wrong with my insertion code. It connects to the database fine but it does not inserts the data.

This is my server.js:

var mongo = require('mongodb').MongoClient
global.db = null
var sDatabasePath = 'mongodb://localhost:27017/kea'
global.mongoId = require('mongodb').ObjectID

/**************************************************/

var student = require(__dirname + '/student.js')

/**************************************************/

mongo.connect(sDatabasePath, (err, db) => {
    if (err) {
        console.log('ERROR 003 -> Cannot connect to the database')
        return false
    }
    global.db = db
    console.log('OK 002 -> Connected to the database')
    return true
})

And this is my student.js:

var student = {}

/**************************************************/

student.saveStudent = (fcallback) => {
    var jStudent =
        {
            "firstName": "Sarah",
            "lastName": "Jepsen",
            "age": 27,
            "courses": [
                {
                    "courseName": "Web-development",
                    "teachers": [
                        {
                            "firstName": "Santiago",
                            "lastName": "Donoso"
                        }
                    ]
                },

                {
                    "courseName": "Databases",
                    "teachers": [
                        {
                            "firstName": "Dany",
                            "lastName": "Kallas"
                        },
                        {
                            "firstName": "Rune",
                            "lastName": "Lyng"
                        }
                    ]
                },
                {
                    "courseName": "Interface-Design",
                    "teachers": [
                        {
                            "firstName": "Roxana",
                            "lastName": "Stolniceanu"
                        }
                    ]
                }
            ]
        }

    global.db.collection('students').insertOne(jStudent, (err) => {
        if (err) {
            var jError = { "status": "error", "message": "ERROR -> student.js -> 001" }
            console.log(jError)
            return fcallback(true, jError)
        }
        var jOk = { "status": "ok", "message": "student.js -> saved -> 000" }
        console.log(jOk)
        return fcallback(false, jOk)
    })
}


 module.exports = student

In the console I only get the database connection message, which is 'OK 002 -> Connected to the database'. I dont´t get back either the jError or jOk message from the user.js file.

Upvotes: 0

Views: 136

Answers (1)

rdRahul
rdRahul

Reputation: 561

Your function does not call the insert function, the saveStudent method needs to be invoked in order to save the function.

var mongo = require('mongodb').MongoClient
global.db = null
var sDatabasePath = 'mongodb://localhost:27017/kea'
global.mongoId = require('mongodb').ObjectID

/**************************************************/

var student = require(__dirname + '/student.js')

/**************************************************/

mongo.connect(sDatabasePath, (err, db) => {
  if (err) {
      console.log('ERROR 003 -> Cannot connect to the database')
      return false
  }
  global.db = db
  console.log('OK 002 -> Connected to the database');

  /*==================== Call the save function ==================*/
  //call the saveStudent to insert entry in database
  student.saveStudent( ( err , resp ) => { //your callback function 
             console.log("handle callback");
  });
  /*======================================*/

  return true
})

Upvotes: 1

Related Questions