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');
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!
source share