我按照教程创建了一个简单的Node / Express / Mongoose REST API。我在一个终端中运行mongod,在另一个终端中运行我的节点应用程序。
我的应用:
// BASE CONFIG
// =========================================================
var express = require('express'),
bodyParser = require('body-parser'),
util = require('util'),
app = express(),
port = process.env.PORT || 8000,
// database
dbURI = 'mongodb://localhost/nodeSpace',
mongoose = require('mongoose'),
db = null,
// models
Ship = require('./models/ship');
// configure use of bodyParser this lets us get data from a post
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// DB SETUP
// =========================================================
mongoose.connect(dbURI);
db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log('Connected to DB!');
})
var testModel = mongoose.model('Test', new mongoose.Schema({name: String}, {bufferCommands: false}));
testModel.find(function (err, res) {
if(err) {
console.log('Error finding test model: ' + err);
}
else {
console.log('Got test model: ' + res);
}
});
当它运行时,mongo报告形成的连接:
2017-03-28T10:44:28.565-0600 I - [conn51] end connection 127.0.0.1:49289 (5 connections now open)
2017-03-28T10:44:28.565-0600 I - [conn50] end connection 127.0.0.1:49288 (5 connections now open)
2017-03-28T10:44:28.565-0600 I - [conn47] end connection 127.0.0.1:49285 (5 connections now open)
2017-03-28T10:44:28.565-0600 I - [conn49] end connection 127.0.0.1:49287 (5 connections now open)
2017-03-28T10:44:28.565-0600 I - [conn48] end connection 127.0.0.1:49286 (5 connections now open)
但是我的应用程序在发现'打电话给我的测试模型。即使数据库没有返回结果,我也希望它只是一个空对象:
/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:129
collection[i].apply(collection, args);
^
TypeError: Cannot read property 'find' of null
at NativeCollection.(anonymous function) [as find] (/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:129:17)
at Query.execFind (/node_modules/mongoose/lib/query.js:1682:20)
at Query.find (/node_modules/mongoose/lib/query.js:204:15)
at Function.find (/node_modules/mongoose/lib/model.js:821:16)
at Object.<anonymous> (/server.js:34:11)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
答案 0 :(得分:3)
问题是将bufferCommands
设置为false。执行此操作时,必须等待与数据库的连接才能发出任何查询(在open
事件处理程序内部,通过将回调传递给mongoose.connect()
,或等待对于要解决的函数返回的承诺。)
答案 1 :(得分:0)
我接受之前的回答,因为报告的症状是正确的。然而,根本原因是一个不同的问题,这个答案可能会有用。
各种本地和远程数据库无限期地挂起在任何类型的查询上。强制执行无缓冲的查询会导致原始问题中报告的问题。我的mongod在应用程序启动时报告了连接但没有触发连接事件。它似乎是一个依赖版本问题。
这里的教程: https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4
引用这些依赖项:
"dependencies": {
"express": "~4.0.0",
"mongoose": "~3.6.13",
"body-parser": "~1.0.1"
}
但是那些包裹的东西根本不起作用。我尝试了本地安装的mongo和免费的云托管安装。都没有奏效。在将依赖关系更新到此后,本地和云安装都会连接并查询而不会出现问题:
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"mongoose": "^4.9.2"
}