使用Nodejs向MongoDb插入数据不起作用

时间:2017-11-05 12:04:07

标签: node.js mongodb

我正在尝试使用Nodejs插入数据,并且我的插入代码有问题。它可以很好地连接到数据库,但不会插入数据。

这是我的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
})

这是我的学生.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

在控制台中我只收到数据库连接消息,即'OK 002 - >连接到数据库'。 我不会从user.js文件中找回jError或jOk消息。

1 个答案:

答案 0 :(得分:1)

您的函数不调用insert函数,需要调用saveStudent方法才能保存函数。

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
})