0

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
  • Use var data = {}; instead of var data = new Array(); Commented Jun 9, 2015 at 14:31

1 Answer 1

1

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);
            }
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for pointing out the errors, but the thing is req.body.data is empty :/
That's because you're instantiating var data with new Array(). It should be var data = {} instead of var data = new Array(). I just tested this myself in Node and the data doesn't translate the way you would expect it to when you use Array(). EDIT: Try console.logging req.body.data after updating var data = {} and report back know what you get.
I applied all changes, saved the files. Deleted all files from the server, and uploaded them again. Now it´s working! Thank you so much!
Now after everything works on the server-side, I think I get an Cross Domain error, because the Ajax call always fails, even on success. Should I open a new thread ?
Yeah, please do. That's a fundamentally different issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.