Canceling the creation of an item node JS

I am trying to put in server code that would cap the number of total objects at 11. I have some code here that isn’t working and I don’t know what I am doing wrong.
/* 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 {Game1} req.item An item to create
  • @returns {Game1|Promise.<Game1>|void} By returning a value you can stop further event propagation and return
  • a specific result to the caller
    */
    Backendless.enablePromises();
    Backendless.ServerCode.Persistence.beforeCreate(‘Game1’, function(req) {
    //add your code here

var query = new Backendless.DataQuery();
query.options = {
pageSize: 11
};
return Backendless.Persistence.of(Game1).find(query).then(result => {
const games = result.data;

if (games.length >= 11)
{
throw (req.item)
}
});
});
Can someone help me?

Your code is correct except two things.

You shouldn’t throw an Object. It should be an Error or a String.

throw new Error('Too many games')
//OR
throw 'Too many games'


//Alternatively you can return rejected Promise
return Promise.reject('Too many games')

Anyway, even throwing an object shouldn’t make it ‘non-working’. It just makes the response less informative (empty error message)

Second thing is that, when you set the pageSize query option to be 11, the response will never contain more than 11 items, so it is worthless to check for games.length > 11

To improve the handler performance you should set the pageSize to 1 and do the validation based on result.totalObjects field

However checking for response items length to be equal 11 is still valid.

Could you please explain what do you mean by ‘the code is not working’ ?
Does it cause some errors ?
Does invoker receive a success response ?

Doesn’t it prevent the game creation ?
If so, are you sure, there are more than 11 records in the table ?

To have more control on your code you should print out the variables values to the console
or what would be event more sufficient to attach some debugger and debug the code line by line in real time. See how to do this here

It does not work in the sense that when the client tries to save an object on the client’s side, it still saves more than 11 objects. Could you show me a full blown example of what you are talking about?

console.log('Retrieving stored games..');


return Backendless.Persistence.of(Game1).find(query).then(result => {
  console.log(`${result.totalObjects} found in the database`);


  if (result.totalObjects > 11)
  {
    console.log('Maximum games reached. Preventing new game creation by throwing an error..')
    throw new Error('Too many games')
  }


  console.log('Validation passed. New game greation allowed')
}

In debug mode is says Error Game1 is not defined.

I put some stuff in the attachment

Well, that’s probably the cause of ‘not working code’

Replace Game1 to ‘Game1’ string when calling Persistence.of

return Backendless.Persistence.of('Game1').then(...

It works, thank you, and I will be buying expansion packs shortly!