400 - Service invocation failed: java.lang.reflect.InvocationTargetException in IOException (14004)


'use strict';

const Requests = require('../models/Requests');
const Messages = require('../models/Messages');
const Groups = require('../models/Groups');
const GroupMessages = require('../models/GroupMessages');
class EventsService {
/**
 * @returns {Promise.<void>}
 */
deleteUserAccount() {
if (this.request.context.userId == null) {
    throw new Error('Please login first!');
}
//Create UserObject
let currentUser = new Backendless.User();
currentUser.objectId = this.request.context.userId;
let groupsStorage = Backendless.Persistence.of(Groups);
let groupMsgsStorage = Backendless.Persistence.of(GroupMessages);
let requestsStorage = Backendless.Persistence.of(Requests);
return new Promise((resolve, reject) => {
//Delete Requests
requestsStorage.bulkDelete("from.objectId = '" + currentUser.objectId + "' OR to.objectId = '" + currentUser.objectId + "'").then(function (res) {
    //Delete all group messages
    groupMsgsStorage.bulkDelete("from.objectId = '" + currentUser.objectId + "'").then(function (res) {
        //Delete all groups where user is owner
        groupsStorage.bulkDelete("owner.objectId = '" + currentUser.objectId + "'").then(function (res) {
            resolve(1);
 }).catch(function (err) {
            log(TAG, JSON.stringify(err));
            reject(err);
        });
    }).then(function (err) {
        log(TAG, JSON.stringify(err));
        reject(err);
    });
}).catch(function (err) {
    log(TAG, JSON.stringify(err));
    reject(err);
})
}
}

I tried to perform this chain in separated code and it works fine. Even first two works ok, and then with third it fails.

In debugger:

12:11:09.291 - [18D436B8-F0E7-140F-FF3D-DE360871AA00] New task arrived!
12:11:09.295 - [18D436B8-F0E7-140F-FF3D-DE360871AA00] [INVOKE SERVICE] services.EventsService.deleteUserAccount
12:11:09 GMT+0100 (CET).800 - EventsService.deleteUserAccount - "0"
12:11:09 GMT+0100 (CET).800 - EventsService.deleteUserAccount - "Requests Deleted"
12:11:10 GMT+0100 (CET).345 - EventsService.deleteUserAccount - "0"
12:11:10 GMT+0100 (CET).345 - EventsService.deleteUserAccount - "Group Messages Deleted"
12:11:10 GMT+0100 (CET).347 - EventsService.deleteUserAccount - undefined
12:11:10.348 - [18D436B8-F0E7-140F-FF3D-DE360871AA00] Processing finished
12:11:10 GMT+0100 (CET).823 - EventsService.deleteUserAccount - "0"
12:11:10 GMT+0100 (CET).823 - EventsService.deleteUserAccount - "Groups Deleted"

You can see that processing ended up early (because of thrown error on your side), even though code ended without error on my side, and therefore response is not (1).

Hi. We’ve created inner task BKNDLSS-16208 and will answer here about the progress.

Hey Tomas

Could you please change “then” to “catch” in your code (see screenshot)

http://support.backendless.com/public/attachments/05a61084c7d3ac73b01d321cfc33ea8d.png</img>

Regards, Vlad

and also what do you think about this optimization ?

 deleteUserAccount() {
 const userId = this.request.context.userId;


 if (!userId) {
 throw new Error('Please login first!');
 }


 const groupsStorage = Backendless.Persistence.of('Groups');
const groupMsgsStorage = Backendless.Persistence.of('GroupMessages');
const requestsStorage = Backendless.Persistence.of('Requests');


 return Promise
 .all([
 requestsStorage.bulkDelete(`from.objectId = '${userId}' OR to.objectId = '${userId}'`),
 groupMsgsStorage.bulkDelete(`from.objectId = '${userId}'`),
 groupsStorage.bulkDelete(`owner.objectId = '${userId}'`),
 ])
 .then(() => 'user account has been removed')
 }

Yes eventually i might optimize it…but i was in dev process and was not sure why this was happening. Thank you for finding my bug, so sorry about that. Consider this closed

ok, no problem

have a nice weekends