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

Invalid JSON on NULL column values, in index.js #10

Open
subatomicglue opened this issue Jul 30, 2018 · 1 comment
Open

Invalid JSON on NULL column values, in index.js #10

subatomicglue opened this issue Jul 30, 2018 · 1 comment

Comments

@subatomicglue
Copy link

subatomicglue commented Jul 30, 2018

.../node_modules/sequelize-json/index.js:28

var Sequelize = require('sequelize');

function JsonField(db, modelName, fieldName, options) {
  var self = this;
  options = options || {};

  process.nextTick(function() {
    function stringifyField(instance) {
      if (typeof instance.dataValues[fieldName] !== 'string' && instance.dataValues[fieldName]) {
        instance.setDataValue(fieldName, JSON.stringify(instance.getDataValue(fieldName)));
        return self;
      } else if (instance.dataValues[fieldName] === 'null' || !instance.dataValues[fieldName]) {
        instance.setDataValue(fieldName, undefined);
      }
    }
    if (typeof db.models === 'object' && db.models.hasOwnProperty(modelName) && typeof db.models[modelName].hook === 'function') {
      db.models[modelName].hook('beforeUpdate', stringifyField);
      db.models[modelName].hook('beforeCreate', stringifyField);
    }
  });

  var model = {
    type: options.type || Sequelize.TEXT,
    get: function() {
      var currentValue = this.getDataValue(fieldName);
      if (typeof currentValue == 'string') {
        try {
        this.dataValues[fieldName] = JSON.parse(currentValue);

Getting this error for emptystring '' values in MySQL JSON columns

err_stack:
SyntaxError: Unexpected end of JSON input\n    
at JSON.parse (<anonymous>)\n    
at model.get (.../node_modules/sequelize-json/index.js:28:43)\n    
at model.get (.../node_modules/sequelize/lib/model.js:3244:41)\n    
at model.get (.../node_modules/sequelize/lib/model.js:3265:33)\n    

Right here, currentValue is empty string (typeof string, and '').

This was working, and this morning I started upgrading from Sequelize 3.30 to 4.38 and found this.

Is it something that needs a check for empty string?

@subatomicglue
Copy link
Author

subatomicglue commented Jul 30, 2018

definitely an error on our side (we should remove this invalid json from our DB),
but thought it was strange that it started happening (and used to work before this morning).

perhaps sequelize used to check for emptystring, safety-checking before calling the sequelize-json getter, and now no longer does...

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

No branches or pull requests

1 participant