Got an error : Cannot save entity with primitive collection property ___dates___

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

Little update.

It works if I intercept the “BeforeCreate” instead of the “AfterCreate” event

This is the new code that I’m using


Backendless.enablePromises();
Backendless.ServerCode.Persistence.beforeCreate('Comments', function(req) {
    var EventsStore = Backendless.Data.of('Events');
    EventsStore.findById(req.item.event).then(event => {
        event.total_comments++;
        EventsStore.save(event);
    }).catch(err => {
        return Promise.reject('Unable to update post comments counter. Got an error : ' + err.message)
    });
}, true);

Another problem… :frowning:

The event is called only if is in debug… In production it does not work.
Any Idea?

We will investigate this problem.
Inner task BKNDLSS-13507.

You do not return a Promise from your event handler

ServerCode works well in the DEBUG mode, but does not when deployed to the Cloud
Sync vs Async code

Please provide your application id

I used another approach and the code run correctly even in production mode:

var Events = require('../../../models/events.js');
Backendless.ServerCode.Persistence.beforeCreate('Comments', function(req) {
 return Events.findById(req.item.event).then(event => event.incrementCommentsCounter()).catch(err => {
 Promise.reject('Unable to update event comments counter. Got an error : ' + err.message);
 });
}, true)

With this code on event.js

 incrementCommentsCounter() {
 this.total_comments++;
 return this.save();
 }

But it works only if you call the “beforeSOMETHING” handler. The “AfterSOMETHING” handler return the same error:

Error: Unable to update event comments counter. Got an error : Cannot save entity with primitive collection property ___dates___

To investigate this issue I need your application id

At the moment I’m non using an after handler, but this is the application id
E218CEB2-9F49-DF20-FF02-AC28A72B1200

It is a bug. I have created an internal ticket.
As a workaround use this:

Backendless.ServerCode.Persistence.afterCreate('Comments', function(req) {
 delete req.item.event.___dates___;
 return req.item.event.incrementCommentsCounter().catch(err => {
 throw new Error('Unable to update event comments counter. Got an error : ' + err.message);
 });
}, true);

Thanks very much.