parse-community / Parse-SDK-JS

The JavaScript SDK for Parse Platform
https://parseplatform.org
Apache License 2.0
1.33k stars 597 forks source link

Can't create an object pointer without any data #1406

Closed alino20 closed 3 years ago

alino20 commented 3 years ago

New Issue Checklist

Issue Description

Can't create pointers without dirty keys hence saving any object which includes these pointers results into cascade save. This is gonna throw an error if this pointer is in another object and user does not have the permission to save this pointer's object so cascade save results into error. Additionally can't create multiple instance of one object (with id) with different dirty keys without saving them because all of them share the same fields.

Steps to reproduce

const MyClass = Parse.Object.extend("MyClass");
let newRow = new MyClass({a: 1})
newRow = await newRow.save();

// Changing the field without save
newRow.set('a', 2)

// create new pointer
let newRowPointer = new MyClass({id: newRow.id})
console.log('a:', newRowPointer.get('a')) 
console.log('new pointer dirtyKeys: ', newRowPointer.dirtyKeys()) 

// or create object without data
let newRowPointer2 = MyClass.createWithoutData(newRow.id)
console.log('a:', newRowPointer2.get('a')) 
console.log('new pointer 2 dirtyKeys: ', newRowPointer2.dirtyKeys()) 

Actual Outcome

Console prints a: 2 and the dirty keys are ['a']

Expected Outcome

Console prints should be a: 1 and the dirty keys should be []

Environment

Server

Database

Client

Logs

github-actions[bot] commented 3 years ago

Thanks for opening this issue!

dplewis commented 3 years ago

See https://github.com/parse-community/Parse-SDK-JS/blob/master/src/ParseObject.js#L71

This is because of the way instances are created in Parse JS depending on your enviornment.

On Web clients there are single / shared instances which means objects with the same id will have same attributes. On Node they are unique instances.

I hope this helps.

alino20 commented 3 years ago

Thanks. I changed my code according to your answer, and now it works as expected. Although I feel this should be somewhere in the documentations.