Search code examples
node.jsnode-oracledb

Unable to bind in Node.js node-oracledb module


I have following code but it just doesn't seem to work. I don't understand what's wrong.

await connection.execute(
`UPDATE mytable SET WEIGHT = :WEIGHT
 WHERE ITEM_ID = :ITEM_ID AND NAME = :NAME`
 ), Bindings[0],{autoCommit: true, outFormat : oracledb.OBJECT};

Where Bindings[0] is:

{
   ITEM_ID: 'dfghjkl',
   WEIGHT: '10',
   NAME: 'test'
}

This yields the following error: Error: ORA-01008: not all variables bound

Please help


Solution

  • This works for me:

    'use strict';
    
    process.env.ORA_SDTZ = 'UTC';
    
    const oracledb = require('oracledb');
    let config = require('./dbconfig.js');
    
    async function run() {
      let connection;
    
      try {
        connection = await oracledb.getConnection(config);
    
        let result;
    
        try {
          result = await connection.execute(`drop table mytable`);
        } catch (e) {
          if (e.errorNum != 942) console.error(e);
        }
    
        await connection.execute(`create table mytable (weight number, item_id varchar2(20), name varchar2(30))`);
    
        await connection.execute(`insert into mytable (weight, item_id, name) values (0, 'dfghjkl', 'test')`);    
    
        let Bindings = [];
        Bindings[0] = {
          ITEM_ID: 'dfghjkl',
          WEIGHT: '10',
          NAME: 'test'
        };
    
        result = await connection.execute(
          `UPDATE mytable SET WEIGHT = :WEIGHT WHERE ITEM_ID = :ITEM_ID AND NAME = :NAME`,
          Bindings[0], {autoCommit: true, outFormat : oracledb.OBJECT});
    
        result = await connection.execute(`select * from mytable`);
        console.log(result.rows);
      } catch (err) {
        console.error(err);
      } finally {
        if (connection) {
          try {
        await connection.close();
          } catch (err) {
        console.error(err);
          }
        }
      }
    }
    
    run();