Backendless Support
 
Answered

beforeCreate JS return Promise not updating item

I need to make some async calls before updating the item. What am I doing wrong? The value of title is not changed to 'CHANGE TITLE'

  1. Backendless.ServerCode.Persistence.beforeCreate('Notification', function(req) {
  2. //add your code here
  3. var CategoryStore = Backendless.Data.of('Category');
  4. var NotificationStore = Backendless.Data.of('Notification');
  5. var channel = req.item.category.channel;
  6. var queryBuilder = Backendless.DataQueryBuilder.create().setWhereClause("channel = '" + channel + "'");
  7. return new Promise((resolve, reject) => {
  8. CategoryStore.find(queryBuilder)
  9. .then(categories => {
  10. req.item.title = 'CHANGE TITLE';
  11. resolve();
  12. })
  13. .catch(err => {
  14. reject('Error retrieving category. Error: ' + err.message);
  15. });
  16. });
  17. }, true);

Leave a Comment

Comments (9)

photo
1

I was able to make it work by putting false instead of true in the second argument.

photo
1

Hello,

Where did that boolean argument come from? I generated the beforeCreate code in my app and do not see it there at all:

1e7b6ddfd670870464a2f3a3e7688912

Regards,

Mark

photo
1

When I created the event.

I am also trying to change the relationship (category) of the object. It doesn't seem to work. Can you help?

  1. /**
  2. * @param {Object} req The request object contains information about the request
  3. * @param {Object} req.context The execution context contains an information about application, current user and event
  4. * @param {Notification} req.item An item to create
  5. */
  6. Backendless.ServerCode.Persistence.beforeCreate('Notification', function(req) {
  7. //add your code here
  8. var CategoryStore = Backendless.Data.of('Category');
  9. var NotificationStore = Backendless.Data.of('Notification');
  10. var channel = req.item.category.channel;
  11. var queryBuilder = Backendless.DataQueryBuilder.create().setWhereClause("channel = '" + channel + "'");
  12. return new Promise((resolve, reject) => {
  13. CategoryStore.find(queryBuilder)
  14. .then(categories => {
  15. if (categories.length == 0) {
  16. CategoryStore.findById({objectId: '09881500-C33F-04CA-FF04-AFA8CDA02700'})
  17. .then(category => {
  18. req.item.category = category;
  19. resolve();
  20. })
  21. .catch(err => {
  22. reject('Could not get default Category. Error: ' + err.message);
  23. });
  24. }
  25. else {
  26. req.item.category = categories[0];
  27. resolve();
  28. }
  29. })
  30. .catch(err => {
  31. reject('Error retrieving category. Error: ' + err.message);
  32. });
  33. });
  34. }, false);

photo
1

Let's keep each topic to one problem, it makes it easier to manage issues this way.

Are you using version 4 of Backendless?

photo
1

Yes.

photo
1

I created a "beforeCreate" event using Backendless console and my code looks different. I'd like to understand where that additional parameter came from. Could you double check please and describe how to reproduce that form of code generation?

photo
1

Hello,

I deleted and re-created the event handler and now I see it like you. Without the boolean second parameter.

Thanks,

photo
1

Ok, good. Does it update the object as expected?

photo
1

I created a new topic for the relation. But for basic object properties, it works.

photo