简体   繁体   中英

Not being able to MongoDB through mongojs in Node

var express = require('express');
var router = express.Router();
var mongojs = require('mongojs');
var db = mongojs('mongodb://******:********@********.mlab.com:*****/XXXXXXXX_XXXX', ['XXXX']);

router.get('/tasks', function(req, res, next){
    db.tasks.find(function(err, tasks){
        if(err){
            console.log('connection error!');
            res.send(err);
        }
        res.json(tasks);
    });
});

module.exports = router;

I have this simple code in node in which I am trying to connect to Mongo. I believe the process is quite straight-forward and I am following it right. Howver I get Authentication failed error every time. I checked the id password and they are just fine. Could someone tell me where I am going wrong?

router.get('/tasks', (req, res)) {
db.tasks.find()
    .then(tasks => res.json(tasks))
    .catch(err => res.status(500).json(err);
});

try that. you had it set up like the get was using a custom middleware component, and you were then passing a function into the .find() call. .find() will get all documents and returns a promise. .then() executes once the promise has been returned, and .catch() executes if .find() had an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM