使用mongoose和express - db.once('open',function(){console.log('connection opened')})可能出现异步错误

时间:2015-01-02 04:30:21

标签: javascript mongodb mongoose

我有一个简单的mongodb快递应用程序

我试图根据使用get请求传递的参数来查询数据库,但是当达到console.log(' == params ==',params)时,console.log(&# 39;连接打开')不是。我怀疑这是一个异步问题,但我无法完全理解为什么我甚至没有传递任何变量

server.js

var express = require('express');
var db = require('./modules/db')
var logic = require('./modules/logic')

var app = express();
app.set('port', (process.env.PORT || 5000));

app.get('/phrase', function(req, res){
  logic(req.query);
  console.log('req.query', req.query)
})

db.js

var mongoose = require('mongoose');
mongoose.connect('...');
var db = mongoose.connection;
module.exports = db;

logic.js

var db = require('./db')

var logic = function(params){
  console.log('==params==', params)
  db.once('open', function(){
    console.log('connection opened')
  })  
}

module.exports = logic;

0 个答案:

没有答案