MongoDb and node js

0

I have an application made in reactjs , I manage the dependencies with nodejs and I want to connect to a database that I have installed locally.

install express and mongodb using npm.

this error is generated when I lift the application:

  

Module not found: 'module' in C: \ Projects \ ZarateAlarms \ zaw \ node_modules \ requir   @ ./~/require_optional/~/resolve-from/index.js 3: 13-30

and my code is:

var MongoClient = require('mongodb').MongoClient;

    MongoClient.connect('mongodb://localhost:27017/animals', function(err, db) {
        if (err) {
            throw err;
        }
        db.collection('mammals').find().toArray(function(err, result) {
            if (err) {
                throw err;
            }
            console.log(result);
        });
    });
    
asked by Gustavo 21.01.2017 в 05:13
source

1 answer

1

Use mongoose , it's very easy to use, and it allows you to map the models.

let Model = require('./models/model');
Model.find({},function(err,data){
//handle error and data
});
    
answered by 28.01.2017 в 16:17