1

I have a problem with converting a JSON in string. On iOS all works perfect but on Android I have this error:

[ERROR] :  TiExceptionHandler: (main) [2,20592] - Message: Uncaught TypeError: Converting circular structure to JSON

This is my code:

var args = $.args

var data = args.data;
var oferta = data.oferta;
var params = data.params;
var parent = args.parent;

//INSERT
var producto = Alloy.createModel('Producto', {
  oferta_id: parseInt(oferta.id),
  data: JSON.stringify(args) //ERROR
});

The args passed to the controller are like this:

var args = {
   data: 
   { 
      oferta: {id: 5}, 
      params:{id_opcion_precio: 3445}
   },
   parent: {}
}

What's wrong?? Why on iOS works fine???

amurcia
  • 801
  • 10
  • 26
  • Why do you need JSON.stringify? Check out this answer: http://stackoverflow.com/questions/4816099/chrome-sendrequest-error-typeerror-converting-circular-structure-to-json – rjcpereira Jun 21 '16 at 09:47
  • Because I want to keep this JSON in a text field on a db table – amurcia Jun 21 '16 at 09:59
  • I saw the answer above before and is not my case, I think. I can't see a circular structure like in the answer – amurcia Jun 21 '16 at 10:00
  • The strange thing is that on iOS works perfectly, if args is an circular structure it should be on iOS too. – amurcia Jun 21 '16 at 10:05
  • Ok, I don't use modules, only database. Try to do a more direct approach when you pass the arguments, instead of create variables to one specific id or value. That code after 'The args passed to the controller are like this:' it's inside the model right? – rjcpereira Jun 21 '16 at 10:12

2 Answers2

0

Not sure why you want to stringify args. Try to use JSON.stringify(args.data) when you create the model.

I guess you would have the same problem just stringify'ing args in a log statement like: console.log(JSON.stringify(args))?

I have run into similar problems when trying to write the entire event (e.g. from ti.map) to the console. Something makes it break - I assume there is added some attributes to the data that you don't see.

John Dalsgaard
  • 2,797
  • 1
  • 14
  • 26
  • Finally I solved my problem doing this: ´JSON.stringify({data: data, parent: parent})´ This works I don't know why, but works – amurcia Jun 22 '16 at 07:08
  • Ok, so similar experience to what I have come across with events. Must be something added that we can't see ;-) – John Dalsgaard Jun 22 '16 at 10:06
0

Finally I solved my problem doing this:

JSON.stringify({data: data, parent: parent});

This works I don't know why, but works

amurcia
  • 801
  • 10
  • 26