JS pass data incorrectly

JS pass data incorrectly

YouLLYouLL Posts: 1Questions: 1Answers: 0
edited September 2020 in Free community support

Hello, I'm facing a problem trying to implement DataTables to my NodeJS project.
Main problem is that the request.body is being passed wrongly, it is an object but it has inside strings (that should be objects), as you can see, this is the console.log(req.body):

As you can see from the image, the various data shouldn't be inside ' ', otherwise my module can't search for the object "request['search']['value']".

This is where I manage the POST routing and take the req.body

routing.post("/getItems", isLoggedIn, (req, res) => {
    let tableName = "items";
    let primaryKey = "name";
    let columns = [];
    columns.push({"db": "name", "dt": "0" }, { "db": "label", "dt": "1" }, { "db": "weight", "dt": "2" }, { "db": "description", "dt": "3" });

    console.log(req.body)

    let ssp = new SSP(connection);
    ssp.simple(req.body, tableName, primaryKey, columns, function (err, response) {
        res.json(response);
    });
});

This is where I declare the object DataTable for my table:

    $("#itemsTable").DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            "url": "/homepage/getItems",
            "type": "POST",
        },
        columns: [{ data: "0" }, { data: "1" }, { data: "2" }, { data: "3" }],
    });

I'm using an NPM Module to handle the server-side processing that it is similar to the one written with PHP.
Thank you.

Answers

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    As you can see from the image, the various data shouldn't be inside ' '

    I think that's just the debugger showing it as a string since the parameter names have [] in them (which is used to indicate the array or object notation).

    What are you using in your node script to parse the request parameters? If body-parser, you might need to set its {extended: true} option.

    Allan

This discussion has been closed.