How to return JSON from MongoDB to Node.js?

I have a mongodb database called pokemon with a collection called pokemons . Here is my attempt to write a function that will perform the find() operation in mongodb:

 '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? }); }; 

Then I call this function in another file:

 var express = require('express'); var router = express.Router(); router.get('/pokedex', function (req, res) { res.jsonp(db.getPokemonByName('Dratini')); }) 

This link helps show how to write mongodb data to the console by executing some sort of each() method on the cursor object, but I don't know how to return json via the getPokemonByName function. I tried to define an empty array in the root area of ​​the getPokemonByName function and insert data into this array with each iteration of the .each show method in this link, but I think I still cannot return this array because it happens after the fact.

By the way, I just do it for fun and learn about MongoDB and Node.js, so I don’t want to use either ODM, like Mongoose, to do some of this for me.

Thanks for any help!

Edit

Just curious, for those reading this, why is this question getting downvoted? Is it because the answer can be obtained from other, similar questions or something else?

Thanks!

+5
source share
3 answers

I was able to answer my question using the node of the monogodb driver github's native page: See here.

In essence, I just defined my exported function in the MongoClient join function. For some reason, I thought that node export should be at the root of the module, but it is not. Here's the finished version:

 '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(); }); }); 

And then in another file:

 var express = require('express'); var router = express.Router(); router.get('/pokedex', function (req, res) { res.jsonp(db.getPokemonByName()); }); 

Of course, this solution requires me to ask hard requests, but this time I'm fine. I will cross this bridge when I come to it.

+5
source

this can help

 var cursor = db.collection('pokemons').find({name:name}).toArray(function(err,arr){ return arr; }); 
0
source

You can use find function callbacks to return json. Try

 exports.getPokemonByName = function (name,callback) { MongoClient.connect(url, function(err, db) { assert.equal(null, err); var cursor = db.collection('pokemons').find({name: name},function(err,result){ if(err) { callback(err,null); } if(result) callback(null,result); }); }); }; router.get('/pokedex', function (req, res) { db.getPokemonByName('Dratini',function(err,result){ if(result) { res.jsonp(result); } }); }) 
0
source

Source: https://habr.com/ru/post/1234989/


All Articles