TypeError:无法读取属性' BSON'未定义的

时间:2017-06-28 07:32:45

标签: javascript node.js mongoose bson

我正在使用PM2部署nodeJS应用程序,该过程开始正常,但是,当我尝试访问该URL时,它会停止该过程,然后给我一个错误的"状态。检查日志,我得到了:

TypeError: Cannot read property 'BSON' of undefined
MyExpenses-0 (err):     at Object.<anonymous>(/home/develop/development/nodejs/expenses/node_modules/mongodb/lib/mongodb/index.js:46:40)
MyExpenses-0 (err):     at Module._compile (module.js:435:26)
MyExpenses-0 (err):     at Object.Module._extensions..js (module.js:442:10)
MyExpenses-0 (err):     at Module.load (module.js:356:32)
MyExpenses-0 (err):     at Function.Module._load (module.js:311:12)
MyExpenses-0 (err):     at Function._load (/usr/local/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:62:21)
MyExpenses-0 (err):     at Module.require (module.js:366:17)
MyExpenses-0 (err):     at require (module.js:385:17)
MyExpenses-0 (err):     at Object.<anonymous> (/home/develop/development/nodejs/expenses/node_modules/mongoose/lib/utils.js:5:16)
MyExpenses-0 (err):     at Module._compile (module.js:435:26)

我使用mongoose进行db。

有任何线索吗?

谢谢!

更新

// server.js

var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');

// Database configuration
var db = require('./config/db');

// Connetion port
var port = process.env.PORT || 3500;

// Connect to our mongo db
mongoose.connect(db.url);

app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride('X-HTTP-Method-Override'));

// Setting router file
require('./app/routes')(app);

// Start app on PORT
app.listen(port);
console.log('Server listening...');

exports = module.exports = app;

我唯一的API网址是:

app.post('/api/expenses/create/', function (req, res) {
    var name = req.body.name;
    var amount = req.body.amount;
    var userId = req.body.userId;
    User.findOne({'_id': userId}, function (err, user) {
        if (!err) {
            var newExpense = Expense({
                name: name,
                amount: amount,
                user_id: userId
            });
            newExpense.save(function (expenseErr) {
                if (!expenseErr) {
                    res.status(200).json({'message': 'Gasto creado correctamente.', expense: newExpense});
                } else {
                    res.status(400).json({'message': expenseErr});
                }
            });
        } else {
            res.status(400).json({'message': 'El usuario seleccionado no existe.'});
        }
    });
});

0 个答案:

没有答案