0

I'm getting the following error when trying to get a JSON data string for charts.js

Uncaught TypeError: Cannot create property 'datasets' on string '{"datasets":[{"label":"My First dataset","data":[{"x":"2017-01-01","y":35},{"x":"2017-01-15","y":28},{"x":"2017-01-31","y":42}]}]}'

The JSON string I'm creating looks exactly the same like the one in an example. Also if I just copy and paste this string and set it as my data it's just working fine.

This is working:

            data: {"datasets":[{"label":"My First dataset","data":[{"x":"2017-01-01","y":35},{"x":"2017-01-15","y":28},{"x":"2017-01-31","y":42}]}]},

This is not working:

.done(function( data ) {
    console.log(data);
        type: 'line',
        data: data });

Any ideas what I am doing wrong here?

2
  • I don't really understand but you probably need to convert the string to javascript object using : JSON.parse(data) Commented Jul 5, 2017 at 10:32
  • You didn't understand the problem but you had the correct solution. Thanks ;-) Commented Jul 5, 2017 at 10:35

1 Answer 1

1

Data received is in string format. You should call JSON.parse(data) on received data in order to access dataset property.

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

1 Comment

Yea that was my mistake. Stupid me. Thanks ;-)

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.