0

I'm trying to create a function that selects data from database in server-side and sets it as value of textbox in client-side. It works fine in server-side, but in client side, if I write a console.log for the data, it shows undefined and I can't figure out why. Thanks a lot !

Server-side:

app.post('/id', function(req,res) {

    var data = req.body;
    var id = data.id;
    console.log(id);
    var query = "SELECT * FROM Control WHERE id=" +id;
    connection.query(query, function(error, result) {
            console.log(result);
            res.json(result);
    });
});

Client-side:

function select()
            {
                var id = $('#nr_reg').val();
                $.ajax({
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    url: '/id',
                    data : {
                        id: id
                    },
                    success : function(data){           
                        console.log(data);                          
                        var id = data.id;
                        $('#optic').val(id);

                    },
                    error: function(err){
                        console.log(err);
                    }

                }); 
            }

The result from server after the query:

5 //this is the id which is correct
[ { id: 5,
    data: '',
    kw: '6',
    nr_articol: '279630-99',
    proiect: 'AUHE',
    optic: '0',
    electric: '',
    reelectric: '',
    smd: 'on',
    scurt_smd: '0',
    incomplete_smd: '',
    bile: '',
    val: '',
    scurt_val: '',
    incomplete_val: '',
    nivel_cositor: '',
    greseli_smd: '',
    lipsa_smd: '',
    invers_smd: '',
    plantare_manuala: '',
    componente_lipsa: '',
    componente_inversate_1: '',
    greseli_hotbar: '',
    lipire_hotbar: '',
    greseli_asamblare: '',
    componente_inversate_2: '',
    greseli_imprimare: '',
    componente_inversate_3: '',
    greseli_aspect: '',
    fire_lovite: '',
    componente_inversate_4: '',
    casaste: '',
    reparate: '',
    Total: '0' } ]
4
  • what does the serverside object look like and what about the client object you are receiving? and what do you expect it to look like? Commented Sep 22, 2015 at 16:24
  • you have two data options in the jquery ajax call. data: JSON.stringify(data) and data: {id: id} Commented Sep 22, 2015 at 16:24
  • looks abit like a mixed post/get request.. Commented Sep 22, 2015 at 16:25
  • I have updated the first post to see how the console shows the result after the query. Commented Sep 22, 2015 at 16:27

1 Answer 1

1

Try use data[0]

Since your outcome is: [{ id: 5,.... }] it means a object in an array. in order to data.id work, your outcome should be {id: 5,.....}. So try this:

            function select()
            {
                var id = $('#nr_reg').val();
                $.ajax({
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    url: '/id',
                    data : {
                        id: id
                    },
                    success : function(data){           
                        console.log(data);                          
                        var id = data[0].id;
                        $('#optic').val(id);

                    },
                    error: function(err){
                        console.log(err);
                    }

                }); 
            }
Sign up to request clarification or add additional context in comments.

1 Comment

you are welcome! the [] means it is in an array, that is why i guessed the data[0]

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.