首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Nodejs到MongoDb的数据插入不起作用

使用Nodejs到MongoDb的数据插入不起作用
EN

Stack Overflow用户
提问于 2017-11-05 12:04:06
回答 1查看 123关注 0票数 0

我试图用Nodejs插入数据,我的插入代码有问题。它可以很好地连接到数据库,但不插入数据。

这是我的server.js:

代码语言:javascript
复制
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
})

这是我的student.js:

代码语言:javascript
复制
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 ->连接到数据库‘。我既没有从jError文件中得到user.js消息,也没有从user.js文件中返回user.js消息。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-05 12:22:04

函数不调用insert函数,需要调用saveStudent方法来保存函数。

代码语言:javascript
复制
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
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47121324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档