无法用Node / Express / Mongodb读取null的属性'find'

我正在按照教程来创build一个简单的Node / Express / Mongoose REST API。 我在一个terminal运行mongod,在另一个terminal运行我的节点应用程序。

我的应用:

// 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) 

但是我的应用在我的testing模型上find了'find'调用。 即使数据库没有返回结果,我也希望它只是一个空对象:

 /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) 

问题是将bufferCommands设置为false。 当你这样做的时候,你必须等待连接数据库,然后才能发出任何查询(在open事件处理程序中,通过将callback传递给mongoose.connect() ,或者等待返回的promise通过该function来解决)。

我接受以前的答案,因为报告的症状是正确的。 然而,根本原因是一个不同的问题,这个答案可能是有用的。

各种本地和远程数据库无限期地挂在任何types的查询上。 强制未缓冲的查询导致原始问题中报告的问题。 我的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和免费的云托pipe安装。 都没有工作。 更新我的依赖关系后,本地和云安装连接和查询没有问题:

 "dependencies": { "body-parser": "^1.17.1", "express": "^4.15.2", "mongoose": "^4.9.2" }