admin管理员组

文章数量:1192366

I have a mongodb database called pokemon with a collection called pokemons. Here is my attempt to write a function that will do a 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? 
  });

};

I then 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 is helpful in showing how to log mongodb data to the console by doing some sort of each() method on the cursor object, but I don't know how to return json through the getPokemonByName function. I tried to define an empty array on the root scope of the getPokemonByName function and push data into that array with each iteration of the .each method show in that link, but I think I still can't return that array because it happens after the fact.

BTW, I'm really just doing this for fun and to learn about MongoDB and Node.js, so I don't want to use or an ODM like Mongoose to do some of this work for me.

I have a mongodb database called pokemon with a collection called pokemons. Here is my attempt to write a function that will do a 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? 
  });

};

I then 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 is helpful in showing how to log mongodb data to the console by doing some sort of each() method on the cursor object, but I don't know how to return json through the getPokemonByName function. I tried to define an empty array on the root scope of the getPokemonByName function and push data into that array with each iteration of the .each method show in that link, but I think I still can't return that array because it happens after the fact.

BTW, I'm really just doing this for fun and to learn about MongoDB and Node.js, so I don't want to use or an ODM like Mongoose to do some of this work for me.

Share Improve this question edited Dec 7, 2019 at 12:19 E_net4 30k13 gold badges111 silver badges151 bronze badges asked Nov 2, 2015 at 3:11 just another profile namejust another profile name 7221 gold badge10 silver badges31 bronze badges 3
  • 1 Possible duplicate of How to return the response from an asynchronous call? – Blakes Seven Commented Nov 2, 2015 at 5:28
  • 1 check it : stackoverflow.com/questions/19696240/… – Alok Deshwal Commented Nov 2, 2015 at 5:46
  • 2 These two links are unrelated. Didn't really get why it is downvoted. – Genc Commented Aug 22, 2016 at 19:40
Add a comment  | 

4 Answers 4

Reset to default 11

I was able to answer my question with help from node's native monogodb driver github page: See here.

In essence, what I did was to define my exported function within the MongoClient's connection function. For some reason I thought node exports had to be in the root of the module, but that's not the case. Here's a 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 that I hardcode queries, but I'm okay with that for now. Will cross that bridge when I come to it.

Found a simple tweak for this. Let say the callback to the findOne returns result then you can convert the result to JSON object like this

result = JSON.parse(JSON.stringify(result))

Now you can access the result and its fields simply with the dot operator.

this may help

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

You can use callbacks on find function to return the 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);
     }
  });

})

本文标签: javascriptHow to return JSON from MongoDB in NodejsStack Overflow