Javascript 如何在Node.js中从MongoDB返回JSON?
我有一个名为Javascript 如何在Node.js中从MongoDB返回JSON?,javascript,json,node.js,mongodb,database,Javascript,Json,Node.js,Mongodb,Database,我有一个名为pokemon的mongodb数据库,其中有一个名为pokemons的集合。下面是我尝试编写的一个函数,该函数将在mongodb中执行find()操作: 'use strict'; var MongoClient = require('mongodb').MongoClient; var assert = require('assert'); // db url var url = 'mongodb://localhost:27017/pokemon'; exports.get
pokemon
的mongodb数据库,其中有一个名为pokemons
的集合。下面是我尝试编写的一个函数,该函数将在mongodb中执行find()
操作:
'use strict';
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
// db url
var url = 'mongodb://localhost:27017/pokemon';
exports.getPokemonByName = function (name) {
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
var cursor = db.collection('pokemons').find({name: name});
// how to return json?
});
};
然后在另一个文件中调用此函数:
var express = require('express');
var router = express.Router();
router.get('/pokedex', function (req, res) {
res.jsonp(db.getPokemonByName('Dratini'));
})
通过对游标对象执行某种each()
方法,有助于显示如何将mongodb数据记录到控制台,但我不知道如何通过getPokemonByName
函数返回json。我试图在getPokemonByName
函数的根作用域上定义一个空数组,并在的每次迭代中将数据推送到该数组中。该链接中显示了每个
方法,但我认为我仍然无法返回该数组,因为它发生在事件之后
顺便说一句,我这么做只是为了好玩,也是为了了解MongoDB和Node.js,所以我不想用Mongoose之类的ODM来帮我做一些这方面的工作 这可能会有帮助
var cursor = db.collection('pokemons').find({name:name}).toArray(function(err,arr){
return arr;
});
您可以使用find函数上的回调返回json。
试一试
在node的原生monogodb驱动程序github页面的帮助下,我能够回答我的问题:
本质上,我所做的是在MongoClient的连接函数中定义导出的函数。出于某种原因,我认为节点导出必须位于模块的根目录中,但事实并非如此。这是一个完整的版本:
'use strict';
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
// db url
var url = 'mongodb://localhost:27017/pokemon';
var findDocuments = function(db, callback) {
// Get the documents collection
var collection = db.collection('pokemons');
// Find some documents
collection.find({name: 'Dratini'}).toArray(function(err, docs) {
assert.equal(err, null);
// assert.equal(2, docs.length);
console.log("Found the following records");
callback(docs);
});
}
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
findDocuments(db, function(docs) {
console.log(docs);
exports.getPokemonByName = function() {
return docs;
}
db.close();
});
});
然后在另一个文件中:
var express = require('express');
var router = express.Router();
router.get('/pokedex', function (req, res) {
res.jsonp(db.getPokemonByName());
});
当然,这个解决方案需要我对查询进行硬编码,但我现在还可以接受。当我走到这一步时,它将跨越这座桥。找到了一个简单的调整方法。假设对findOne的回调返回结果,那么您可以像这样将结果转换为JSON对象
result=JSON.parse(JSON.stringify(result))
现在,您只需使用点运算符即可访问结果及其字段。但是getPokemonByName(name)
函数仍然不会返回任何内容。请检查编辑。现在使用callback返回json。看起来应该可以工作,但会产生循环引用错误:TypeError:将循环结构转换为json
。无法说明原因。可能重复检查:这两个链接是不相关的。没有真正理解为什么它被否决。不,您不能从异步调用内联返回值。这在直接使用mongo shell查询MongoDB时有效。然而,正如Blakes提到的,从数据库获取结果的实际过程是异步的。使用Node和Mongo库的最新版本,您应该能够通过await
:let results=await db.collection('pokemons').find({name}).toArray()实现这一点代码>。这是rad。非常感谢。
var express = require('express');
var router = express.Router();
router.get('/pokedex', function (req, res) {
res.jsonp(db.getPokemonByName());
});