jeshaitan
jeshaitan

Reputation: 301

mongolab request returning empty find results

I have a website that is accessing a local node.js server (that accesses a mongolab database), but when I use a front-end function to request a user JSON object, the mongo database returns nothing. (JSON.parse() finds an unexpected end of data at line 1 col 1)

Here is the front-end function that requests the user data by email and password:

function requestUser(email, password) {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", "http://localhost:8888/getUser/" + email + "/" + password, true);
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
           user = JSON.parse(xmlhttp.responseText);
           console.log(user);
        }
    }
    xmlhttp.send();
}

Here is the node.js express server (back-end):

 var http = require("http"),
    mongojs = require("mongojs"),
    express = require('express'),
    cors = require('cors'),
    fs = require("fs"),
    url = require("url");

app = express();
app.use(cors());

var uri = "mongodb://<dbuser>:<dbpassword>@ds036698.mongolab.com:36698/alirodatabase";
var db = mongojs(uri, ["Papers", "Users"]);

app.get('/getUser/:email/:passwd', function(req, res, next) {
  var users = db.Users.find({"email": req.params.email,
                            "password": req.params.passwd});
  user = users.toArray[0];
  res.json(user);
});

app.listen(8888, function() {
  console.log('CORS-enabled web server listening on port 8888');
});

EDIT 1:

app.get('/getUser/:email/:passwd', function(req, res, next) {
    var user = db.Users.findOne({
        "email": req.params.email,
        "password": req.params.passwd
    }, function(err, doc) {
            if (err) {
                res.json({error: 'error retrieving the JSON user' });
            }
            else {
                res.json(doc);
            }
    });
});

I added async functionality to the nodeserver, but now I am receiving the err: "error retrieving the JSON user". Is this a problem that could be solved by hosting my own database and not using mongolab?

Upvotes: 0

Views: 150

Answers (1)

Randy
Randy

Reputation: 4391

You need to look at the docs for mongojs (https://github.com/mafintosh/mongojs). You're not using callbacks at all. The functions don't return values because it's Javascript/Node.js where things like to be async. So you have to use callbacks to handle the results. The idea is "find these documents" and then some time later when it actually gets the documents "do something with the documents".

app.get('/getUser/:email/:passwd', function(req, res, next) {
    var users = db.Users.find({
        "email": req.params.email,
        "password": req.params.passwd
    }, function(err, docs) {
        if (err) {
            //handle the error
            res.json({error: ':(' });
        }
        else {
            docs.toArray(function(err, users) {
                if (err)  {
                    //handle the error
                    res.json({error: ':(' });
                }
                else {
                    res.json(users[0]);
                }
            });
        }
    });
});

Lastly, I'd recommend using findOne rather than find. Then you won't need to use toArray to get a single document because it's returned as a single document in the first callback.

Upvotes: 1

Related Questions