"Script error. (line 0)" can't see why

"Script error. (line 0)" can't see why

AmbientAmbient Posts: 23Questions: 9Answers: 0

http://live.datatables.net/tabodawo/1/edit?html,css,js,console,output

I am trying to build a table from external api to get exchange rates.

Can't see wht the cause is.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    Open the browser's developer tools console and you will see this error:

    jquery.dataTables.js:119 Uncaught TypeError: Cannot read property 'length' of undefined

    Looking at the JSON response using the browser's network inspector you are getting this response:

    {
        "base": "EUR",
        "rates": {
            "GBP": 0.86198,
            "HKD": 9.229,
            .........
            "USD": 1.1883,
            "HUF": 366.51,
            "AUD": 1.5491
        },
        "date": "2021-03-23"
    }
    
    

    Its a single object not an array of objects. So you will need to use row.add(), note their is no s, to add a single row. You don't have a data object so you will just add the json not json.data. See the updated example:
    http://live.datatables.net/tabodawo/2/edit

    Kevin

  • AmbientAmbient Posts: 23Questions: 9Answers: 0

    Thank you Kevin. I am still new to all this but making some progress. I very much appreciate your help. However, why is the rates showing as object. How can I get that to display properly?

  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    Answer ✓

    However, why is the rates showing as object. How can I get that to display properly?

    It's because it's an object containing currency values - so you'll need to use columns.render to determine how to display it. This here just turns it into a string, for example - http://live.datatables.net/tabodawo/6/edit

    Colin

  • AmbientAmbient Posts: 23Questions: 9Answers: 0

    Thank you Colin. That was very helpful.

This discussion has been closed.