-1

I'm learning parse with javascript sdk, so this is the first time I want to save data in my databrowser, this is a form of a clase name "Empleado".

 form action="" id="nuevo-empleado">

<label>Introduzca el nombre del empleado</label>
<input type="text" id="nombre"><br>

<label>Introduzca el apellido del empleado</label>
<input type="text" id="apellido"><br>

<label>Introduzca el email del empleado</label>
<input type="email" id="email"><br>

<label>Introduzca el celular del empleado</label>
<input type="tel" id="celular"><br>

<label>Introduzca el teléfono del empleado</label>
<input type="tel" id="telefono"><br>

 <input type="submit" value="Guardar" id="guardar">
 </form>

  <div id="message"></div>
     <script type="text/javascript">
      //create a subclass for empleado
       var Empleado = Parse.Object.extend("Empleado");
         // create a instance for empleado
         var empleado= new Empleado()

        function GuardarEmpleado (argument) {

         var nombre = $("#nombre").val();
         var apellido = $("#apellido").val();
         var email = $("#email").val();
         var telefono = $("#telefono").val();
         var celular = $("#celular").val();

         empleado.set("Nombre", nombre);
         empleado.set("apellido", apellido);
         empleado.set("Email", email);
         empleado.set("Telefono",telefono)
          empleado.set("Celular", celular);


    empleado.save( null, {
        success: function(empleado){
           $("#message").append('Se han guardado los datos del empleado')

        },

        error: function(empleado, error){
             alert('Failed to create new object, with error code: ' + error.message);
        }

    });
}

$("#guardar").on("click", function(e) {
    /* Act on the event */
    e.preventDefault();
    GuardarEmpleado();
});

When I clic the button Guardar, it gives me the next error: "Fail to save Celular, expected number, got a string"

atwalsh
  • 3,622
  • 1
  • 19
  • 38

1 Answers1

0

All data from DOM inputs are regarded as strings when you collect them with jQuery using .val(). If you need that variable to be regarded as a number instead you can use the parseInt() javascript function. http://www.w3schools.com/jsref/jsref_parseint.asp

e.g.

empleado.set("Celular", parseInt(cellular));

Note you could also use parseFloat() if your number requires decimals. You may also want to do some error checking to make sure the input was a valid number Validate decimal numbers in JavaScript - IsNumeric()

Community
  • 1
  • 1
user1573618
  • 1,696
  • 2
  • 20
  • 38