Javascript TypeError:无法读取未定义的属性“execute”。node.js如何导出oracle db连接

Javascript TypeError:无法读取未定义的属性“execute”。node.js如何导出oracle db连接,javascript,node.js,oracle,express,node-modules,Javascript,Node.js,Oracle,Express,Node Modules,您好,我是node和oracle的新手。我已创建了一个应用程序并成功连接到db。 我需要在整个应用程序中使用连接对象,我如何才能做到这一点 下面是我的index.js文件 const express = require("express"); const app = express(); const authRoute = require("./routes/auth"); app.use(express.json()); app.use("/api",authRoute) ; app.li

您好,我是node和oracle的新手。我已创建了一个应用程序并成功连接到db。 我需要在整个应用程序中使用连接对象,我如何才能做到这一点

下面是我的index.js文件

const express = require("express");
const app = express();

const authRoute = require("./routes/auth");
app.use(express.json());
app.use("/api",authRoute) ;

app.listen(3000,function(){
    console.log("Node Server : Running on port 3000...");
})
数据库连接文件=>connect.js

const oracledb = require('oracledb');
const dotenv = require('dotenv');
dotenv.config();

const connection = oracledb.getConnection(
    {
      user          : process.env.USER,
      password      : process.env.PASS,
      connectString : process.env.ConnectString
    },
    function(err, connection)
    {
        if (err) {
        console.error(err.message);
        return;
    }
    console.log('Connection was successful!');
    connection.close(function(err){
        if (err) {
        console.error(err.message);
        return;
        }
    });
});
module.exports = connection;
我想在auth.js文件中使用此db连接

const router = require('express').Router();
const db = require('../database/connect');


router.post("/authenticate",function(req,res){

    //console.log(req);
    const user = req.body.username;
    const username = {"name" : user};
    const pass = req.body.key;
    const password  = {"pass" : pass};
    //const result = db.execute('select * from usertable');// this doesn't work
    //console.log(result.rows);
    res.send('success');
});

module.exports = router;
当我运行const result=db.execute'select*from usertable';我得到下面的错误

TypeError: Cannot read property 'execute' of undefined

我做错了什么。请任何人帮忙。请提前感谢使用在应用程序启动时打开的连接池。然后可以使用池缓存获取池,然后获取其他模块中的连接

对于像您这样的web应用程序,您肯定希望使用连接池来提高性能

中有一个关于连接池的重要部分。例如,看哪一个说:

创建池时,可以为池指定一个命名别名。别名可以 稍后将用于检索相关池对象以供使用。这 促进跨模块共享池并简化获取 联系


这些都值得回顾。

我已经阅读了这些示例。但无法进行训练。据我所知,我的连接在生成之前已被导出。如何在我的代码中使用回调?。提前感谢look at和@SwapnilShende,如果您能分享我面临的相同问题的解决方案,我将不胜感激