0

I've got an object:

var val = {username:'gilbertbw',password:'password',password2:'password',email:'email@email.com',firstname:'gilbert',lastname:'BW',test:true};

I run some validation against it and then post it to my node.js server:

console.log(
            $.ajax({
                type: 'POST',
                url: '/signup',
                data: val,
                success: function(){console.log('data posted')},
                dataType: "json"
            })
        );

On the server i have tryed just calling the object val:

console.log(val.username);

and pulling it from the post like I normally would:

val = req.param(val,null);
console.log(val.username);

But both times firebug spits out the 500 ISE:

    "{"error":{"message":"val is not defined","stack":"ReferenceError: val is not defined at exports.signup (C:\\Users\\Gilbert\\WebstormProjects\\NodeOfGames\\routes\\user.js:37:21)

If i just console.log(val); it prints null The line in question is when I try to log val.username to console. How do you recive a posted object in node.js?

gilbertbw
  • 634
  • 2
  • 9
  • 27

1 Answers1

0

val is a client-side variable. there is no way for the server to know you named this object "val".

data: val is just like:

data: {username:'gilbertbw',password:'password',password2:'password',email:'email@email.com',firstname:'gilbert',lastname:'BW',test:true}

so there is no val field for your server to find.

do this instead:

data: {
    "val": val
}

edit:

also, your line req.param(val,null); should be req.param("val",null);
or val = req.body.val;
(reference)

EyalAr
  • 3,160
  • 1
  • 22
  • 30
  • the value of `val` is still `null` on the server, is there anything special I have to do to pick `val` up on the server? – gilbertbw Aug 08 '12 at 18:00
  • I still get `val is not defined` but with `console.log(req.body.val)` I get the expected responce in the server log. so `val = req.body.val` works fine for me – gilbertbw Aug 08 '12 at 18:39
  • glad it works. it'd be nice if you accept it as the correct answer. – EyalAr Aug 08 '12 at 19:20