【发布时间】:2015-07-29 13:32:40
【问题描述】:
我正在使用 Cloude 9 环境来开发我的 nodejs 应用程序。在那我已经编写了连接到 mongodb 数据库的代码。我成功连接到数据库并将记录添加到集合中。
现在我想发送收藏信息作为回报。但是使用res.send(collectionInfo); 不起作用。
让我知道我应该如何实现这一目标
下面是我的 server.js 文件的代码
var Db = require('mongodb').Db;
var http = require('http');
var path = require('path');
var async = require('async');
var socketio = require('socket.io');
var express = require('express');
var ejs = require('ejs');
var app = express();
var helpers = require('express-helpers')
var MongoClient = require('mongodb').MongoClient;
var Server = require('mongodb').Server;
var db;
helpers(app);
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({extended: true})); // for parsing application/x-www-form-urlencoded
var server = http.Server(app);
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function () {
var addr = server.address();
console.log("Chat server listening at", addr.address + ":" + addr.port);
});
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
//app.use(express.static(__dirname + '/client'));
app.use(express.static(path.join(__dirname, '/client')));
// MongoDB Connection
app.use(function(req, res, next) {
next();
})
app.get('/monogdb', function (req, res) {
res.render('monogdb.ejs');
});
app.post('/ajax-mongo-connect', function (req, res) {
var mongoClient = new MongoClient(new Server('localhost', 27017));
mongoClient.open(function(err, mongoClient) {
if(err){
console.log(err);
}else{
var db = mongoClient.db("mydb");
db.createCollection("students", { name : req.body.nm, description : req.body.desc, location : req.body.loc } );
console.log('database connected',db);
var collectionInfo = db.collection("students");
mongoClient.close();
//res.setHeader('Content-Type', 'application/json');
res.send(collectionInfo);
}
})
})
根据@Roman Sachenko 的回答,我尝试使用
res.send(collectionInfo.toJSON()); 但它给出以下错误
/home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/db.js:299
throw err;
^
TypeError: Object #<Collection> has no method 'toJSON'
at /home/ubuntu/workspace/server.js:66:41
at MongoClient.open
(/home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/mongo_client.js:103:5)
at Db.open (/home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/db.js:296:11)
at process._tickCallback (node.js:442:13)
使用res.send({data: collectionInfo}); 会出错
home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/db.js:299
throw err;
^
TypeError: Converting circular structure to JSON
at Object.stringify (native)
at ServerResponse.res.json (/home/ubuntu/workspace/node_modules/express/lib/response.js:185:19)
at ServerResponse.res.send (/home/ubuntu/workspace/node_modules/express/lib/response.js:117:21)
at /home/ubuntu/workspace/server.js:67:21
at MongoClient.open (/home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/mongo_client.js:103:5)
at Db.open (/home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/db.js:296:11)
at process._tickCallback (node.js:442:13)
【问题讨论】:
-
您应该对 process.env.PORT 很好,但请注意,您只能在 Cloud9 上公开访问端口 8080、8081 和 8082,因此请确保您的错误与端口使用无关。
-
您是否尝试按照我在回答中的建议进行操作?
-
重要一点,您为什么要退回收藏?您应该在调用集合后执行某些操作,例如插入或删除......当您调用集合函数时,基本上 mongodb 返回一个准备在数据库中执行操作的结构。我建议你检查你的代码。如果您需要更多帮助,我在这里 ;)
-
@roman-sachenko 建议的代码永远不会工作,因为是 Mongoose 框架的代码,您使用的是 MongDB 原生框架。
-
@mujaffars 我更新了我的答案。看看那里。
标签: node.js mongodb cloud9-ide