Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json array becomes key of json object (and fails to parse) #315

Closed
coolaj86 opened this issue Jun 12, 2014 · 10 comments
Closed

json array becomes key of json object (and fails to parse) #315

coolaj86 opened this issue Jun 12, 2014 · 10 comments

Comments

@coolaj86
Copy link
Contributor

There is a json field cards that contains an array. When the model serializes it serializes the array as the key to an object with no value and causes a parse error:

{ [error: invalid input syntax for type json]
  name: 'error',
  length: 192,
  severity: 'ERROR',
  code: '22P02',
  detail: 'Expected ":", but found "}".',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: 'JSON data, line 1: ...kR1PAJ\\",\\"customerId\\":\\"cus_4D10fjjmkR1PAJ\\"}"}',
  file: 'json.c',
  line: '1075',
  routine: 'report_parse_error' }

This error is actually from the pg library, so maybe the problem is actually there. How would I see the SQL right before it's handed off to pg?

@tgriesser
Copy link
Member

Assuming you're on the latest, if you do:

queryChain.on('query', function(data) {
   console.log(data.sql);
   console.log(data.bindings);
}).then(function() {
  // ...
});

you should see the data that will be passed off for the query.

@tgriesser
Copy link
Member

Also, judging from the output there, it looks like maybe the JSON is already stringified rather than in object form?

@coolaj86
Copy link
Contributor Author

Query:

update "customer" set "credit" = ?, "creditcards" = ? where "row_id" = ?

Bindings:

[ 0,
  [ { id: 'card_104D1S2r5QTT3HO4rYng5QEL',
      object: 'card',
      last4: '4242',
      type: 'Visa',
      exp_month: 11,
      exp_year: 2020,
      fingerprint: '8KD3JPLFk9j58bbG',
      country: 'US',
      name: 'AJ ONeal',
      address_line1: '',
      address_line2: '',
      address_city: null,
      address_state: null,
      address_zip: null,
      address_country: null,
      cvc_check: 'pass',
      address_line1_check: 'pass',
      address_zip_check: null,
      customer: 'cus_4D1SbsV2No6oHl',
      customerId: 'cus_4D1SbsV2No6oHl' } ],
  1 ]

I'm assuming that the statement and bindings are what you're passing directly to pg, so the fault is there?

@tgriesser
Copy link
Member

Oh, indeed it is. See brianc/node-postgres#442, brianc/node-postgres#374, bookshelf/bookshelf#94

So I think it looks like pre-stringifying the data will do the trick?

@coolaj86
Copy link
Contributor Author

I tried stringifying and it didn't work, so I tried wrapping in an object and I discovered this strange behavior with parse and format.

Here's what I've got:

Models.Customer = PostgreSql.Model.extend({
  parse: function (obj) {
    if ('creditcards' in obj) {
      obj.creditcards = obj.creditcards.dummy || [];
    }
    console.log('parse Customer', obj);
    return obj;
  }
, format: function (obj) {
    if ('creditcards' in obj) {
      obj.creditcards = { dummy: obj.creditcards || [] };
    }
    console.log('format Customer', obj);
    return obj;
  }
});

However, format is being called directly after parse for some strange reason, and so I keep getting the nested object with the dummy when calling user.get('creditcards').

@tgriesser
Copy link
Member

Pretty sure that was a regression added here: bookshelf/bookshelf#377

Just fixed it and cut 0.7.2 - see if that gets you what you're looking for.

elliotf pushed a commit to elliotf/knex that referenced this issue Nov 24, 2014
@CaptainJojo
Copy link

It's close but does'nt work, is in my model I send array of json , I have 'error syntax'

@rhys-vdw
Copy link
Member

Hi @CaptainJojo. Can you please open this as a new issue including code that generates the error, and pass debug() to log the generated query.

@sielay
Copy link

sielay commented Mar 3, 2016

Any updates on this one? My case

{
 name: 'my page',
     path: 'page/template',
     javascripts: [ 'js1', 'js2' ],
     stylesheets: [ 'css1', 'css2' ] } }

does

insert into "template" ("javascripts", "name", "path", "stylesheets") values ('{"js1","js2"}', 'my page', 'page/template', '{"css1","css2"}') 

that ends up as

insert into "template" ("javascripts", "name", "path", "stylesheets") values ($1, $2, $3, $4) returning "id" - invalid input syntax for type json 

@sielay
Copy link

sielay commented Mar 3, 2016

Anyhow stringify before save does the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants