1
votes

I have problems with afterSave implementing my signup logic. What I have to do is to add a transaction in Wallet class when user makes the registration, giving him some credits. So i have my cloud code function:

// Add welcome bonus on signup
Parse.Cloud.afterSave(Parse.User, (request) => {
// commit transaction only on signup completed with phone number
  if (request.object.get('username')) {
    const wallet = new Parse.Object('Wallet')

    wallet.set('value', 100)
    wallet.set('action', '+')
    wallet.set('description', 'welcome bonus')
    wallet.set('user', {
       __type: 'Pointer',
       className: '_User',
       objectId: request.object.id
    })


    wallet.save(null,{useMasterKey: true}).then(
      (result) => console.log('objectId',result.id),
      (error) => console.log('code:',error.code,'message:',error.message)
    )
  }
})

Since Parse.Cloud.useMasterKey() is deprecated I follwed the Parse doc on how to use the useMasterKey option on save method, but i still got this error:

info: afterSave triggered for _User for user undefined:
  Input: {...} className=_User, triggerType=afterSave, user=undefined
code: 107 message: Received an error with invalid JSON from Parse: Cannot POST /classes/Wallet

And this is my Wallet table:


| value: Number | action: String | description: String | user: Pointer<_User> |


And the default columns createdAt, updatedAt, objectId and ACL

I'm working on localhost.

Any ideas on what's going on?

1
cant u just use wallet.save() without the parameters inside? if that doesnt work, try adding {useMasterKey: true} at the top of your code - Lyon
Can you please make sure that value is an Integer type and action column is a string type ? Maybe you already saved some object before and pass them different types. - Ran Hassid
Can you show me the Wallet table ? - Julien Kode
Checked types of my columns and they are number and string. - altafan
{useMatserKey: true} is an option passed in methods like save() or find(). It's not like Parse.Clous.useMasterKey (deprecated) that had to be put at the top of my function - altafan

1 Answers

0
votes

I would bet this is an issue with the way you're setting the User. If the field is set to be a pointer field to a user object, all you have to do is set wallet.set('user', request.object); The rest of the request looks fine, so my gut tells me it just isn't liking that JSON format for your set. I.e. when I set a pointer on the dashboard, I just type in the object ID. I don't format it like a pointer.

Also, this is gonna get triggered any time you save a user, so it isn't going to do what you want beyond this issue anyway.