Express js 4, Parse.com JSON error

127 views Asked by At

I am trying to send a POST call to an Express JS server hosted on Parse.com. I send the data like this:

var data = new Array();     
    data["firstName"] = firstName;
    data["lastName"] = lastName;
    data["dateOfBirth"] = dateOfBirth;
    data["mobileNumbe"] = mobileNumber;
    data["email"] = email;
    data["reEmail"] = reEmail;
    data["pw"] = pw;
    data["rePw"] = rePw;

    $.ajax({
            url: "/api/post/registerclient",
            type: "POST",
            dataType: "application/json",
            data: { data:data
                  },
            success: function(result){
                disabledButton("buttonRegister",false);
                if(data.success == true){
                    console.log("Neu registriert");
                }else{
                    showErrorMessageWithError(result.error);
                }
            },
            error:function(error){
                disabledButton("buttonRegister",false);

                var string = "Es ist ein Fehler aufgetreten!"
                showErrorMessageWithError(string);
            }
        });

On the server side, I try to catch the data with req.body.data, but it is empty:

exports.registerClient = function(req,res){

//Data, get = firstName, lastName, dateOfBirth, mobileNumber, email, reEmail, pw, rePw
//Data, respond = success, error
var validation = helper.validateRegisterClient(req.body.data);


if(validation.length == 0){
    var user = new Parse.User();
    user.set("username", req.data.email);
    user.set("password", req.data.pw);
    user.set("email", req.data.email);

    //optional fields
    user.set("mobileNumber", req.data.mobileNumber);
    user.set("firstName", req.data.firstName);
    user.set("lastName", req.data.lastName);
    user.set("dateOfBirth", req.data.dateOfBirth);

    user.signUp(null, {
        success: function(user) {
            // Hooray! Let them use the app now.
            var success = true;
            var error = "";

            var json = {success: success, error: error};
            res.json(json);
        },
        error: function(user, error) {
            var success = false;

            var json = {success: success, error: error};
            res.json(json);
        }
    });
}else{
    var success = false;
    var error = validation;

    var json = {sucess: success, error: error};
    res.json(json);
}
}

I have the feeling, that my express app may be configured wrong, so here is my "init":

var express = require('express');
var parseExpressHttpsRedirect = require('parse-express-https-redirect');
var parseExpressCookieSession = require('parse-express-cookie-session');
var app = express();

// Global app configuration section
app.set('views', 'cloud/views');  // Specify the folder to find templates
app.set('view engine', 'ejs');    // Set the template engine
app.use(parseExpressHttpsRedirect());
app.use(express.bodyParser());    // Middleware for reading request body
app.use(express.cookieParser('xxxxxxxxxxxxxxxxxxxx'));
app.use(parseExpressCookieSession({ cookie: { maxAge: 3600000 } }));
1

There are 1 answers

5
richardgirges On BEST ANSWER

There's a typo in your express code. You're referencing req.data when it should be req.body.data:

For instance, this:

var user = new Parse.User();
user.set("username", req.data.email);
user.set("password", req.data.pw);
user.set("email", req.data.email);

Should be this:

var user = new Parse.User();
user.set("username", req.body.data.email);
user.set("password", req.body.data.pw);
user.set("email", req.body.data.email);

Also, your initial data array is actually an object and should be instantiated differently. So this:

var data = new Array();

Should be this:

var data = {};

One other thing I noticed just now is that you're referencing data.success in your ajax success callback. data.success doesn't exist in this scope. This should be result.success.

So this:

            if(data.success == true){
                console.log("Neu registriert");
            }else{
                showErrorMessageWithError(result.error);
            }

Should be:

            if(result.success == true){
                console.log("Neu registriert");
            }else{
                showErrorMessageWithError(result.error);
            }