导出mongodb失败

时间:2017-04-19 20:02:36

标签: node.js mongodb express

我尝试按以下格式导出Mongodb实例propertyCorrected = np.zeros(allTimes.size) propertyCorrected [np.searchsorted(allTimes, loggedTimes)] = property propertyCorrected #array([ 1500., 2000., 500., 0., 75., 60., 0., 0., # 45., 37., 0.]) 。但我无法得到它。

文件夹结构

db

这些是我的个人档案

mongoclient.js

catering
  -server
    -server.js
    -mongoclient.js
  -package.json

Server.js

var MongoClient = require('mongodb').MongoClient;  
var _db;
var exec = require("child_process").exec; 
var async = require('async');
var  series = require('async/series');
var file = __dirname + '/server.js';


async.series([
   function(callback){
       MongoClient.connect( "mongodb://localhost:27017/catering",                 function( err, db ) {
        _db = db;
        db.createCollection('posts');
        module.exports = {db:db};
        // callback(db + "11111");
        callback(null,123)
    });
  },
  function(callback){
    exec('nodemon '+ file,{},function(err,res){
        if(err){
            callback(null,err)
        }else{
            callback(null,res);
        }
    })
  }
],function(err,res){
  console.log("===========res",res,err)
})

的package.json

var express = require("express");
var app = express();
var fs = require('fs');
var path = require("path");
var db = require('./mongoclient.js');

app.listen(4000);

app.get('/',function(req,res){
  console.log("00000000000000000000get",db);    
    res.sendFile(path.join(__dirname + '/../client/index.html'))
});

1 个答案:

答案 0 :(得分:0)

我使用@Aagam链接解决了上述问题。这是更新的代码

module.exports.init = function (callback) {
  var server = new mongodb.Server("127.0.0.1", 27017, {});
     new mongodb.Db('catering', server, {w: 1}).open(function (error, client) {
        module.exports.client = client;
       if(client)
         callback(client,undefined)
       else
         callback(undefined,error);
    });
}; 

Server.js

db.init(function(res,err){console.log("----------data",res); console.log("----------------err",err)})