Support Topics Documentation Slack YouTube Blog

Problems with Transaction API in CloudCode

Dear Backendless team,
I’m trying to build a custom event handler that would use the Backendless V6 transaction feature. I ran into a couple of problems.

Backendless Version (3.x / 5.x, Online / Managed / Pro )

6.0.4

Application ID

3B8C2D2E-B7D6-27F9-FF3F-ABEE20934E00

Description

  1. The addRelation API seems not working. When I called unitOfWork.addRelation in event handler code, it keeps throwing missing ) after argument list error.

  2. For some API that didn’t throw error, when the code is running in cloud, it will gives Backendless.UnitOfWork() is not a constructor error. I copied the exact same code from document. const unitOfWork = new Backendless.UnitOfWork();.

Hello,

Do you use JS or Java?

Could you please show how you invoke the addRelation call?

Regards,
Mark

Hi Mark,

I’m using JS. My code is quite basic. Just like this:

Backendless.ServerCode.customEvent('sendFollow', function(req) {
	var senderId = req.args["senderId"]
	var receiverId = req.args["receiverId"]
	Backendless.Data.of( "Users" ).findById( {objectId:receiverId } )
	 .then( function( user ) {
	 	const unitOfWork = new Backendless.UnitOfWork();
    	unitOfWork.addRelation(	tableName: "Users",
    							parentObjectId: senderId,
    							columnName: "followingPending",
    							childrenObjectId: [receiverId] )
	    unitOfWork.execute()
		.then( function( unitOfWorkResult ) {
			console.log("transaction completed")
		 })
		.catch( function( error ) {
			console.log("something wrong with the transaction")
		 });
	  })
	 .catch( function( error ) {
 		console.log("findby Failed")
 		console.log(error)
	  });
});

When I run npm run debug, the code throws missing ) after argument list error.
Thanks!

Hi Evan,

Make sure to change the name of the method to addToRelation. I think it is the culprit of the errors. I will make sure the docs are updated as well to reflect this.

Regards,
Mark

Hi Mark,

Thanks for your reply. Unfortunately, on my end, using addToRelation isn’t changing anything. The error remains. Still missing ) after argument list.

Evan

Hi Evan,

I do not see anything wrong with your code. It must be something within the SDK.

@stanislaw.grin, could you please take a look?

Regards
Mark

Hi @Evan_Cui,

missing ) after argument list error is a JS syntax error because you’re passing neither object nor the simple arguments list in the ‘addRelation’ method.

Here is how it should be:

unitOfWork.addRelation('Users', senderId, 'followingPending',[receiverId])

Also, there’re no addRelation method - but there is addToRelation.
So, your code should look next way:

Backendless.ServerCode.customEvent('sendFollow', function(req) {
  const senderId = req.args['senderId']
  const receiverId = req.args['receiverId']

  Backendless.Data.of('Users').findById({ objectId: receiverId })
    .then(function(user) {
      const unitOfWork = new Backendless.UnitOfWork()
      
      unitOfWork.addToRelation('Users', senderId, 'followingPending', [receiverId])
      
      unitOfWork.execute()
        .then(function(unitOfWorkResult) {
          console.log('transaction completed')
        })
        .catch(function(error) {
          console.log('something wrong with the transaction')
        })
    })
    .catch(function(error) {
      console.log('findby Failed')
      console.log(error)
    })
})

Regards,
Stanislaw

Hi @stanislaw.grin ,

Thank you for your reply. You are right, the new function call doesn’t throw any error now when I run npm run debug.

However, it still causes the second problem as I described in the description at the beginning. When this event handler receives any requests, it throws TypeError: Backendless.UnitOfWork is not a constructor. I copied the exact same code as you posted here. Is there any problem with the way I initialize the unitOfWork instance?

Thanks!
Evan

Hi Evan,

Could you try updating your local installation of Coderunner? I suspect running npm i should do the trick.

Regards,
Mark

Hi @mark-piller,

Yes, I did that yesterday. I just did it again. I ran npm i and also npm i backendless-coderunner and now it’s version 5.5.0. Seems all packages are up-to-date. Yet the problem remains.

Best
Evan

Hi Evan,

The latest version of backendless-coderunner is 6.0.0. You can see it here:

Please make sure your installation is using that version.

Regards,
Mark

Hi @mark-piller

Thanks. Simply npm i didn’t install the latest coderunner for me somehow. I specify the version explicitly. And it worked.

Thanks a lot!
Evan