MariaDB导出结果后返回未定义。

huangapple go评论78阅读模式
英文:

MariaDB is returning undefined once I export the results

问题

我正在开发一个小型的Express js应用,使用MariaDB

每当我开始查找如何在我的应用中使用MariaDB时,我总是找到一些直接将连接查询直接写入路由的例子。

我认为这样不够可读,正在尝试将它们分开:

  • 路由调用查询(例如User.getAll()
  • User.mjs导入的User.getAll()返回来自数据库的响应。

我的问题:

到目前为止,我认为需要这样:

User.mjs

const User = {
    all: async () => {
        let conn;
        try {
            conn = await pool.getConnection()
            const rows = await conn.query(`SELECT * FROM users`);

            console.log(rows) // 在终端中显示预期的列表
            return rows // 不会返回到调用它的地方..

        } catch (err) {
            throw err;
        } finally {
            if (conn) return conn.end();
        }
    }
}

export default User;

然后在我的用户路由中使用如下:

userRouter.mjs

router.get('/', async (req, res) => {
    const data = await User.all();
    console.log(data); // 在终端中显示"undefined"
    // 最终也会有 res.json(data);
})

查询是有效的...

当我在我的路由中调用函数时,它确实执行了查询,我的**console.log(rows)**显示了我想要的内容。

但是我的来自路由的console.log(data)返回undefined

我有遗漏什么吗?还是这不是一个好的方法?感谢您的时间。

英文:

I am working on a small Express js app, using MariaDB

Whenever I started looking around on how to use mariaDB in my app, I always found examples where the connection and queries are directly written into the routes.

I thought this was not very readable and am trying to seperate the two.

  • route calls for the query (e.g. User.getAll())
  • The User.getAll() which is being imported from User.mjs returns the response from the DB.

My problem:

So far, this is how I thought it would need to be:

User.mjs

const User = {
    all: async () => {
        let conn;
        try {
            conn = await pool.getConnection()
            const rows = await conn.query(`SELECT * FROM users`);

            console.log(rows) // Shows list in terminal as expected
            return rows // does not return to where it's being called..

        } catch (err) {
            throw err;
        } finally {
            if (conn) return conn.end();
        }
    }
}

export default User;

Which is then being used in my User router as follows:

userRouter.mjs

router.get('/', async (req, res) => {
    const data = await User.all();
    console.log(data); // Shows "undefined" in terminal
    // Eventually also res.json(data);
})

The query works...

When I call my function in my router, it does execute the query and my console.log(rows) shows exactly what I want.

But my console.log(data) from my router returns undefined.

Am I missing something? Or is this not a good way of doing it? Thank you for your time.

答案1

得分: 2

The return inside the finally block is overwriting the return from inside the try block. And since conn.end() returns a Promise that resolves without an argument, you get undefined when awaiting it.

一个更简单的示例显示了相同的问题:

function f() {
  try {
    return 1;
  } finally {
    return;
  }
}
console.log(f());
英文:

The return inside the finally block is overwriting the return from inside the try block. And since conn.end() returns a Promise that resolves without an argument, you get undefined when awaiting it.

A simpler example that shows the same issue:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function f() {
  try {
    return 1;
  } finally {
    return;
  }
}
console.log(f());

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月7日 22:51:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定