我正在学习一个教程,以创建一个简单的Node/Express/Mongoose。我在一个终端运行单神,在另一个终端运行我的节点应用程序。
我的应用程序:
// 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)发布于 2017-03-28 17:33:48
问题是将bufferCommands设置为false。当您这样做时,您必须等待建立到数据库的连接,然后才能发出任何查询(要么在open事件处理程序中,要么通过向mongoose.connect()传递回调,要么等待该函数返回的承诺得到解决)。
发布于 2017-03-28 22:46:12
我接受先前的答案,因为它对报告的症状是正确的。然而,根本原因是另一个问题,这个答案可能是有用的。
各种本地和远程数据库无限期地挂在任何类型的查询上。强迫未缓冲的查询导致了原始问题中报告的问题。当应用程序启动时,我的蒙神报告了连接,但没有触发连接事件。这似乎是一个依赖版本控制问题。
这里的教程: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"
}https://stackoverflow.com/questions/43075681
复制相似问题