I'm following this book addyosmani - backbone-fundamentals for creating a simple backbone.js app with node server and mongodb as backend.
我正在关注这本书addyosmani - backbone-fundamentals,用于创建一个带有节点服务器和mongodb作为后端的简单backbone.js应用程序。
As instructed, I've installed the latest versions of Node.js fromnodejs.org and mongodb from www.mongodb.org and ran mongodb by followed the instructions.
按照说明,我已经从www.nongjb.org安装了最新版本的Node.js fromnodejs.org和mongodb,并按照说明运行了mongodb。
The package.json
is as follows:
package.json如下:
{
"name": "backbone-library",
"version": "0.0.1",
"description": "A simple library application using Backbone",
"dependencies": {
"express": "~3.1.0",
"path": "~0.4.9",
"mongoose": "~3.5.5",
"body-parser": "~1.9.1"
}
}
The tutorial then suggests to add the following to server.js
file for connection to mongoDB:
然后,教程建议将以下内容添加到server.js文件以连接到mongoDB:
//Connect to database
mongoose.connect( 'mongodb://localhost/library_database' );
//Schemas
var Book = new mongoose.Schema({
title: String,
author: String,
releaseDate: Date
});
//Models
var BookModel = mongoose.model( 'Book', Book );
I've got confused with the following line:
我对以下行感到困惑:
mongoose.connect( 'mongodb://localhost/library_database' );
because the book doesn't mention anything regarding creating a database named library_database
and I haven't created manually - Still if I run the following in server.js
:
因为这本书没有提到有关创建名为library_database的数据库的任何内容,而且我没有手动创建 - 仍然如果我在server.js中运行以下内容:
mongoose.createConnection( 'mongodb://localhost/library_database' );
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
// we wait till mongo is ready before letting the http handler query users:
db.once('open', function () {
console.log('Running');
//Schemas
var Book = new mongoose.Schema({
title: String,
author: String,
releaseDate: Date
});
//Models
var BookModel = mongoose.model('Book', Book);
});
It logs Running
..! (how come because I didn't create any library_database
..?)
它记录运行..! (为什么我没有创建任何library_database ..?)
But, If I further follow the book by adding a GET request handler as shown below:
但是,如果我通过添加GET请求处理程序进一步遵循本书,如下所示:
//Get a list of all books
app.get( '/api/books', function( request, response ) {
return BookModel.find( function( err, books ) {
if( !err ) {
return response.send( books );
} else {
return console.log( err );
}
});
});
and hit it with the following request:
并使用以下请求点击它:
jQuery.get( '/api/books/', function( data, textStatus, jqXHR ) {
console.log( 'Get response:' );
console.dir( data );
console.log( textStatus );
console.dir( jqXHR );
});
According to the tutorial I'm supposed to see the logs, Instead I get the following error:
根据教程我应该看到日志,而是我得到以下错误:
GET http://localhost:4711/api/books/ net::ERR_EMPTY_RESPONSE
The DB is currently listening to default port with the default database "test".
DB当前正在使用默认数据库“test”监听默认端口。
I tried use library_database
in mongo shell, but still get the same response from the node server.
我尝试在mongo shell中使用library_database,但仍然从节点服务器获得相同的响应。
Do I need to manually create a db for the application..? If so, how can I create an instance for the app and connect to it from node server..?
我是否需要为应用程序手动创建数据库..?如果是这样,我如何为应用程序创建一个实例并从节点服务器连接到它?
Or does mongoose automatically creates such a db in the applications root folder (very unlikely to happen)..?
或者mongoose会自动在应用程序根文件夹中创建这样的数据库(不太可能发生)..?
I'm pretty new to node.js
, mongodb
, mongoose
etc so I might be missing some basic concepts. What am I missing..?
我是node.js,mongodb,mongoose等的新手,所以我可能会遗漏一些基本概念。我错过了什么..?
4 个解决方案
#1
1
I just found the examples for connect
and createConnection
in the docs:
我刚刚在docs中找到了connect和createConnection的示例:
mongoose.connect('mongodb://user:pass@localhost:port/database');
//-----------------------------------------------^
The one in tutorial is as follows:
教程中的一个如下:
mongoose.connect( 'mongodb://localhost/library_database' );
It is missing the port number. I changed it by adding default port number as follows:
它缺少端口号。我通过添加默认端口号来更改它,如下所示:
mongoose.createConnection('mongodb://localhost:27017/library_database');
and now I'm getting the response!
现在我得到了回复!
#2
1
You might find this helpful from mongoose documentation
您可能会从mongoose文档中发现这有用
Important! If you opened a separate connection using mongoose.createConnection() but attempt to access the model through mongoose.model('ModelName') it will not work as expected since it is not hooked up to an active db connection. In this case access your model through the connection you created:
重要!如果使用mongoose.createConnection()打开单独的连接但尝试通过mongoose.model('ModelName')访问模型,则它将无法正常工作,因为它未连接到活动的数据库连接。在这种情况下,通过您创建的连接访问您的模型:
var conn = mongoose.createConnection('your connection string') , MyModel = conn.model('ModelName', schema) , m = new MyModel; m.save(); // works vs
var conn = mongoose.createConnection('你的连接字符串'),MyModel = conn.model('ModelName',schema),m = new MyModel; m.save(); //作品vs
var conn = mongoose.createConnection('your connection string') , MyModel = mongoose.model('ModelName', schema) , m = new MyModel; m.save(); // does not work b/c the default connection object was never connected
var conn = mongoose.createConnection('你的连接字符串'),MyModel = mongoose.model('ModelName',schema),m = new MyModel; m.save(); //无效b / c默认连接对象从未连接过
#3
0
1) You do not need to create a database explicitly. If a database doesn't exist, MongoDB will automatically create one, when the first record is inserted into any collection.
1)您不需要显式创建数据库。如果数据库不存在,当第一条记录插入任何集合时,MongoDB将自动创建一个数据库。
2) I think you just need to insert some books into the collection (through the mongo shell, or bootstrap it in your code) and you should be A for away. The empty response you are getting is because there are no books yet, in the collection, so my guess is you're sending a null in: return response.send( books );
2)我认为你只需要在集合中插入一些书籍(通过mongo shell,或者在你的代码中引导它),你就应该成为A.你得到的空响应是因为在集合中还没有书籍,所以我猜你正在发送一个null:return response.send(books);
#4
0
This is how I have connected.
这就是我的联系方式。
var mongoose = require('mongoose/');
var db = mongoose.connect('mongodb://localhost/newdbcoll');
//e.g. mongodb://username:server@mongoserver:10059/somecollection
//mongoose_auth: 'mongodb://admin:admin@127.0.0.1:27017/chrome-server'
And I am able to connect.
我能够连接。
#1
1
I just found the examples for connect
and createConnection
in the docs:
我刚刚在docs中找到了connect和createConnection的示例:
mongoose.connect('mongodb://user:pass@localhost:port/database');
//-----------------------------------------------^
The one in tutorial is as follows:
教程中的一个如下:
mongoose.connect( 'mongodb://localhost/library_database' );
It is missing the port number. I changed it by adding default port number as follows:
它缺少端口号。我通过添加默认端口号来更改它,如下所示:
mongoose.createConnection('mongodb://localhost:27017/library_database');
and now I'm getting the response!
现在我得到了回复!
#2
1
You might find this helpful from mongoose documentation
您可能会从mongoose文档中发现这有用
Important! If you opened a separate connection using mongoose.createConnection() but attempt to access the model through mongoose.model('ModelName') it will not work as expected since it is not hooked up to an active db connection. In this case access your model through the connection you created:
重要!如果使用mongoose.createConnection()打开单独的连接但尝试通过mongoose.model('ModelName')访问模型,则它将无法正常工作,因为它未连接到活动的数据库连接。在这种情况下,通过您创建的连接访问您的模型:
var conn = mongoose.createConnection('your connection string') , MyModel = conn.model('ModelName', schema) , m = new MyModel; m.save(); // works vs
var conn = mongoose.createConnection('你的连接字符串'),MyModel = conn.model('ModelName',schema),m = new MyModel; m.save(); //作品vs
var conn = mongoose.createConnection('your connection string') , MyModel = mongoose.model('ModelName', schema) , m = new MyModel; m.save(); // does not work b/c the default connection object was never connected
var conn = mongoose.createConnection('你的连接字符串'),MyModel = mongoose.model('ModelName',schema),m = new MyModel; m.save(); //无效b / c默认连接对象从未连接过
#3
0
1) You do not need to create a database explicitly. If a database doesn't exist, MongoDB will automatically create one, when the first record is inserted into any collection.
1)您不需要显式创建数据库。如果数据库不存在,当第一条记录插入任何集合时,MongoDB将自动创建一个数据库。
2) I think you just need to insert some books into the collection (through the mongo shell, or bootstrap it in your code) and you should be A for away. The empty response you are getting is because there are no books yet, in the collection, so my guess is you're sending a null in: return response.send( books );
2)我认为你只需要在集合中插入一些书籍(通过mongo shell,或者在你的代码中引导它),你就应该成为A.你得到的空响应是因为在集合中还没有书籍,所以我猜你正在发送一个null:return response.send(books);
#4
0
This is how I have connected.
这就是我的联系方式。
var mongoose = require('mongoose/');
var db = mongoose.connect('mongodb://localhost/newdbcoll');
//e.g. mongodb://username:server@mongoserver:10059/somecollection
//mongoose_auth: 'mongodb://admin:admin@127.0.0.1:27017/chrome-server'
And I am able to connect.
我能够连接。