Andrés Chamorro Andrés Chamorro - 3 months ago 21
Node.js Question

Oracle delete statement is not working in JS

I'm trying to delete a row from a table in an Oracle database using this JS function:

deleteDirection(directionId, callback) {
if (!this.connected) {
return Promise.reject(new Error('not connected')).asCallback(callback)
}
const connection = this.connection

const task = Promise.coroutine(function* () {
const conn = yield connection
const query = 'DELETE FROM directions WHERE direction_id = :directionId'
const result = yield conn.execute(query, { directionId }, {
outFormat: oracle.OBJECT,
})
.then(directionDeleted => {
if (directionDeleted.rowsAffected === 0) {
return Promise.reject(new Error('invalid number'))
}
return Promise.resolve(directionDeleted)
})
.catch(err => Promise.reject(new Error(err)));

return Promise.resolve(result)
})
return Promise.resolve(task()).asCallback(callback)
}


The function finish without any problems but the row is not deleted, if I try the same statement on Oracle SQL Developer it works pecfectly, What could be the problem?

Answer

Try this:

deleteDirection(directionId, callback) {
if (!this.connected) {
  return Promise.reject(new Error('not connected')).asCallback(callback)
}
const connection = this.connection

const task = Promise.coroutine(function* () {
  const conn = yield connection
  const query = 'DELETE FROM directions WHERE direction_id = :directionId'
  const result = yield conn.execute(query, { directionId:  directionId}, {
    outFormat: oracle.OBJECT,
    autoCommit: true
  })
  .then(directionDeleted => {
    if (directionDeleted.rowsAffected === 0) {
      return Promise.reject(new Error('invalid number'))
    }
    return Promise.resolve(directionDeleted)
  })
  .catch(err => Promise.reject(new Error(err)));

  return Promise.resolve(result)
})
return Promise.resolve(task()).asCallback(callback)
}

Notice the change to the bind variable and the use of autoCommit.

Are you using a transpiler?