Skip to content

Parse.Object.fromJSON creates empty record in DB #482

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

Closed
andrekosak opened this issue Sep 19, 2017 · 4 comments
Closed

Parse.Object.fromJSON creates empty record in DB #482

andrekosak opened this issue Sep 19, 2017 · 4 comments
Assignees

Comments

@andrekosak
Copy link

andrekosak commented Sep 19, 2017

SDK package version: 1.10.0

When I use Parse.Object.fromJSON on NodeJS server, record are being created containing only objectId, createdAt, updatedAt. All other properties i have passed in my JSON are being ignored.

In the example below I am trying to create an object with properties a and b, but they are not being passed to server. At first I create an object and then getting this object by its ID to see if it has my properties.

Here is my code:

const testObject = {
    a: 1,
    b: 2,
    className: 'TestOnly1'
}
const test = Parse.Object.fromJSON(testObject);
test.save()
    .then((newObject) => {
        console.log(JSON.stringify(newObject, null, 3));

        const query = new Parse.Query('TestOnly1')
        query.get(newObject.id)
            .then((queryResponse) => {
                console.log(JSON.stringify(queryResponse, null, 3));
            })
    });

console output from first console.log():

{
   "a": 1,
   "b": 2,
   "createdAt": "2017-09-19T09:48:58.174Z",
   "updatedAt": "2017-09-19T09:48:58.174Z",
   "objectId": "fqJV15MOLK"
}

output from second console.log():

{
   "createdAt": "2017-09-19T09:48:58.174Z",
   "updatedAt": "2017-09-19T09:48:58.174Z",
   "objectId": "fqJV15MOLK"
}

Is it a bug or i am doing something wrong?

@dplewis
Copy link
Member

dplewis commented Mar 11, 2018

I saw this as well. Look like fromJSON wasn't designed to save data.

  • save only works on dirty fields i.e obj.set() that have yet to be committed and not in the cache.
  • fromJSON saves fields in the cache.
  • Your example ends up with no dirty fields to be saved since all the data is cache.
  • save.then(newObject) just returning what was in the cache

@flovilmart Is this by design or for offline use?

@flovilmart
Copy link
Contributor

I’m not sure, i’ll Have to investigate more, starting with when it was added :)

@stale
Copy link

stale bot commented Feb 6, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Feb 6, 2019
@stale stale bot closed this as completed Feb 13, 2019
@francojohnc
Copy link

I check the code and no set called from parse object

static fromJSON(json
  /*: any*/
  , override
  /*:: ?: boolean*/
  ) {
    if (!json.className) {
      throw new Error('Cannot create an object without a className');
    }

    const constructor = classMap[json.className];
    const o = constructor ? new constructor() : new ParseObject(json.className);
    const otherAttributes = {};

    for (const attr in json) {
      if (attr !== 'className' && attr !== '__type') {
        otherAttributes[attr] = json[attr];
      }
    }

    if (override) {
      // id needs to be set before clearServerData can work
      if (otherAttributes.objectId) {
        o.id = otherAttributes.objectId;
      }

      let preserved = null;

      if (typeof o._preserveFieldsOnFetch === 'function') {
        preserved = o._preserveFieldsOnFetch();
      }

      o._clearServerData();

      if (preserved) {
        o._finishFetch(preserved);
      }
    }

    o._finishFetch(otherAttributes);

    if (json.objectId) {
      o._setExisted(true);
    }

    return o;
  }

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

4 participants