'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).