venter venter - 1 month ago 5
TypeScript Question

Returning a bluebird promise after sequilize db operation

I got a function that should delete a DB row. The function gets the id of the row that should be deleted. Also the function should return the deleted row to perform some text outputs.

deleteProcess(processToDeleteId: any): Promise<ProcessInstance> {
let deletedProcess;
let value;

for (let key in processToDeleteId) {
if (processToDeleteId.hasOwnProperty(key)) {
value = processToDeleteId[key];
}
}

this.sequelize.query("select * from Processes where processId = ?",
{ replacements: [value], type: this.sequelize.QueryTypes.SELECT })
.then(function (processes) {
for (let item in processes) {
if (processes.hasOwnProperty(item)) {
deletedProcess = processes[item];
console.log(deletedProcess);
}
}

})
.delay(20);

let that = this;
return new Promise<ProcessInstance>(
(function (resolve, reject) {
that.sequelize.query("delete from Processes where processId = ?",
{ replacements: [value], type: that.sequelize.QueryTypes.DELETE })
.then(function () {
console.log('Deleted successfully');
return deletedProcess;
})
reject(function (err) {
console.log(err);
})
})
)
};


The issue is the function works well in therms of deleting the item, also it loads the row that is deleted, but doesn't returns the row

Answer

You never call resolve, which you need to if you want to populate a promise created through the promise constructor. That said, I don't believe you need the promise constructor here.

Does it work if you do this?

    return that.sequelize.query("delete from Processes where processId = ?",
        { replacements: [value], type: that.sequelize.QueryTypes.DELETE })
        .then(function () {
            console.log('Deleted successfully');
            return deletedProcess;
        })
        .catch(console.log)

By the way, you don't need to do let that = this; if you use an arrow function

Comments