mardi 11 août 2015

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');
});




Aucun commentaire:

Enregistrer un commentaire