I am having a problem pulling the data called, to my table. How can i achieve this?

I am having a problem pulling the data called, to my table. How can i achieve this?

kmartinkmartin Posts: 7Questions: 1Answers: 0

This is my api code

app.get('/readVendor', function (req, res) {
let sql = "SELECT Id,firstName,lastName,telephone,email,address FROM vendors";
//query db
con.query(sql,
function (err, result) {
if (err) {
res.status(500).send({ message: err });
} else if (result) {
res.status(200).send({data: result});
}
});
})

Here is the result when called

{
"data": [
{
"Id": 1,
"firstName": "karim",
"lastName": "james",
"telephone": " 111-345-6789",
"email": "",
"address": "Constitution+Hi"
},
{
"Id": 14,
"firstName": "karim",
"lastName": "james",
"telephone": " 111-345-6789",
"email": "arim@gmail.com",
"address": "Constitution+Hi"
},
{
"Id": 17,
"firstName": "karim",
"lastName": "james",
"telephone": " 111-345-6789",
"email": "garim@gmail.com",
"address": "Constitution+Hi"
},
{
"Id": 18,
"firstName": "karim",
"lastName": "james",
"telephone": " 111-345-6789",
"email": "gadrim@gmail.com",
"address": "Constitution+Hi"
},
{
"Id": 19,
"firstName": "karim",
"lastName": "james",
"telephone": " 111-345-6789",
"email": "gdrim@gmail.com",
"address": "Constitution+Hi"
},
{
"Id": 20,
"firstName": "karim",
"lastName": "james",
"telephone": " 111-345-6789",
"email": "gdrie@gmail.com",
"address": "Constitution+Hi"
},
{
"Id": 21,
"firstName": "karim",
"lastName": "james",
"telephone": " 111-345-6789",
"email": "gdaje@gmail.com",
"address": "Constitution+Hi"
},
{
"Id": 22,
"firstName": "karim",
"lastName": "james",
"telephone": " 111-345-6789",
"email": "gdaffe@gmail.com",
"address": "Constitution+Hi"
},
{
"Id": 23,
"firstName": "karim",
"lastName": "james",
"telephone": " 111-345-6789",
"email": "gadeffe@gmail.com",
"address": "Constitution+Hi"
}
]
}

This is my datatable.js code

$(document).ready(function () {
$('#example').DataTable({
ajax:{
url: 'http://localhost:3005/readVendor',
},
"columns": [
{ "data": "Id" },
{ "data": "firstName"},
{ "data": "lastName" },
{ "data": "telephone"},
{ "data": "email"},
{ "data": "address"}
],
});
});

I am not sure what's going on, but I need your help.

Answers

  • kmartinkmartin Posts: 7Questions: 1Answers: 0

    the above picture is a browser view of the data called.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    It should work, see here. Are you able to link to your page so we can take a look?

    Colin

  • kmartinkmartin Posts: 7Questions: 1Answers: 0
    edited December 2019

    @colin I tried that solution before and it worked and that's what makes things weird.

    When I called the data from a text file or from the javascript, it worked.

    But when I called the data from an express.js API, the data don't show in the table.

    If I select (F12) and look under the network tab I can see that the data was called.

  • kmartinkmartin Posts: 7Questions: 1Answers: 0

    @colin i am wondering if its a timing issue.

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    If I select (F12) and look under the network tab I can see that the data was called

    Do you see the response and is it the above data?

    Do you see errors in your browser's console?

    Can you provide a link to your page so we can take a look?

    Kevin

  • kmartinkmartin Posts: 7Questions: 1Answers: 0

    Do you see the response and is it the above data? Yes
    Do you see errors in your browser's console? No
    Can you provide a link to your page so we can take a look? Not at this moment, I am using my node.js as my server on my laptop. I would have to post the site on a public webserver

  • kmartinkmartin Posts: 7Questions: 1Answers: 0

    @kthorngren my problem is rendering the data on the table.

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    It looks like everything is correct. If you are unable to provide a test case maybe the developers can find something interesting from the debugger.

    Kevin

  • allanallan Posts: 63,538Questions: 1Answers: 10,476 Site admin

    Yes - I don't see anything wrong there, so I'm not sure why it isn't working. If you could use the debugger as Kevin says, I'll take a look at it.

    Allan

  • kmartinkmartin Posts: 7Questions: 1Answers: 0

    thanks @kthorngren and @allan. I tried something else i think its how react setup

This discussion has been closed.