0

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.

1
  • where are you calling student.saveStudent method ?? you need to invoke the method. Commented Nov 5, 2017 at 12:11

1 Answer 1

1

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
})
Sign up to request clarification or add additional context in comments.

2 Comments

also how can I return the data which has been inserted in the db to the console?
Just modify your callback in insertOne as ( err , result ) instead of just (err). Then you can log that result

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.