node-oracledb Error: NJS-069 in nuxt middleware

node-oracledb Error: NJS-069 in nuxt middleware

我可以访问数据库并从我的 Oracle 数据库中获取数据。但是这个错误发生在我的浏览器控制台中。

import oracledb from 'oracledb'

oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT

export default async function (ctx) {
  let connection

  try {
    connection = await oracledb.getConnection({
      user: 'user',
      password: 'user!1',
      connectString: '127.0.0.1:1521/uat2'
    })
    const result = await connection.execute(
      `SELECT PAGETITLE, ACTIVITYID
       FROM TB_MOULD
       WHERE ACTIVITYID = 'WYDJQHD001'`,
      []
    )
    console.log(result.rows)
  } catch (err) {
    console.error(err)
  } finally {
    if (connection) {
      try {
        await connection.close()
      } catch (err) {
        console.error(err)
      }
    }
  }
}

我猜 webpack 在 bundle 过程中不区分 node-oracledb 是否可以 运行 在非服务器端。 有人有解决方案吗?

来自node-oracledb 5.0 changelog:

Webpack users should copy the node-oracledb binary into a sub-directory of the output directory. For example if the output directory is dist, then the binary should be in dist/node_modules/oracledb/build/Release/oracledb-5.0.0-linux-x64.node. A copy plugin in webpack.config.js can do this by copying node_modules/oracledb/build to a directory of that same name. See Issue 1156.

一个可能需要回顾的事情是 ES 模块的使用——我没有在这方面尝试过它们。

我已经找到解决办法了

  if (process.server) {
    const oracledb = require('oracledb')
    oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT
    //......
  }