Hello,
I’m trying to create a custom event that intercept the “AfterCreate” of “Comments” table to increment a counter on the “Events” table that is linked with a 1-to-1 relationship.
I’m using javascript generated code.
this is the custom event:
/* global Backendless */
/**
* @param {Object} req The request object contains information about the request
* @param {Object} req.context The execution context contains an information about application, current user and event
* @param {Comments} req.item An item to create
*
* @param {Object} res The response object
* @param {Comments} res.result Created item
* @param {Object} res.error
*/
Backendless.enablePromises();
Backendless.ServerCode.Persistence.afterCreate('Comments', function(req, res) {
var EventsStore = Backendless.Data.of('Events');
return EventsStore.findById(req.item.event)
.then(event => {
event.total_comments++;
return EventsStore.save(event);
})
.catch(err => {
return Promise.reject('Unable to update event comments counter. Got an error : ' + err.message)
});
}, true);
This is the log:
12:48:48.541 - Waiting for Server Code tasks..
12:49:05.623 - [742493C7-C7CE-DD25-FF63-AD69972CA900] New task arrived!
12:49:05.631 - [742493C7-C7CE-DD25-FF63-AD69972CA900] [INVOKE EVENT HANDLER] persistence.afterCreate (Comments, async)
12:49:06.405 - Error: Unable to update event comments counter. Got an error : Cannot save entity with primitive collection property ___dates___
12:49:06.406 - [742493C7-C7CE-DD25-FF63-AD69972CA900] Processing finished
These are the involved models that are auto generated from backendless
EVENT.JS
/* global Backendless */
'use strict';
class Events extends Backendless.ServerCode.PersistenceItem {
constructor() {
super();
/**
@name Events#total_males
@type Number
*/
this.total_males = undefined;
/**
@name Events#name
@type String
*/
this.name = undefined;
/**
@name Events#cover_image
@type String
*/
this.cover_image = undefined;
/**
@name Events#place
@type Places
*/
this.place = undefined;
/**
@name Events#description
@type String
*/
this.description = undefined;
/**
@name Events#rating
@type EventRating
*/
this.rating = undefined;
/**
@name Events#event_category
@type EventCategories
*/
this.event_category = undefined;
/**
@name Events#end_time
@type Number
*/
this.end_time = undefined;
/**
@name Events#total_females
@type Number
*/
this.total_females = undefined;
/**
@name Events#start_time
@type Number
*/
this.start_time = undefined;
}
}
module.exports = Backendless.ServerCode.addType(Events);
COMMENT.JS
/* global Backendless */
'use strict';
class Comments extends Backendless.ServerCode.PersistenceItem {
constructor() {
super();
/**
@name Comments#total_likes
@type Number
*/
this.total_likes = undefined;
/**
@name Comments#user
@type UserProfiles
*/
this.user = undefined;
/**
@name Comments#event
@type Events
*/
this.event = undefined;
/**
@name Comments#image
@type String
*/
this.image = undefined;
/**
@name Comments#text
@type String
*/
this.text = undefined;
}
}
module.exports = Backendless.ServerCode.addType(Comments);
Thanks in advance
Michele