简体   繁体   English

我的MySQL陈述中需要诺言吗?

[英]Do I need the promise in my MySQL statement?

Below is a MySQL statement in my Node.js app. 以下是我的Node.js应用程序中的MySQL语句。 I used a promise in MySQL function to get API endpoint to work. 我在MySQL函数中使用了promise来使API端点正常工作。 Is this a typical pattern for Node.js and MySQL? 这是Node.js和MySQL的典型模式吗?

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

app.use(express.static('client'));
const config = require('./config')

var mysql = require('mysql');

var con = mysql.createConnection({
    host: config.HOST,
    user: config.USER,
    password: config.PASSWORD,
    database: config.DATABASE
});

function GetConsumers(req, res) {
    return new Promise(function (resolve, reject) {
        con.connect(function (err) {
            if (err) throw err;
            con.query("SELECT * FROM " + config.DATABASE + ".Contracts", function (err, result, fields) {
                if (err) throw err;
                //console.log(result);
                resolve(result);
            });
        });
    }).then(rows => res.send(rows));
}

app.get('/consumers', GetConsumers);

module.exports = app;

As George commented, you don't really need to return a promise here. 正如George所评论的那样,您实际上不需要在这里退还诺言。

function GetConsumers(req, res) {

    con.connect(function (err) {
        if (err) {
           res.send(err)
        };
        con.query("SELECT * FROM " + config.DATABASE + ".Contracts", function (err, result, fields) {
            if (err) {
               res.send(err)
           };
            //console.log(result);
            res.send(result)
        });
    });

} }

If you really want to use promises, it is always a good practice to catch the exceptions. 如果您确实要使用Promise,则捕获异常始终是一个好习惯。

function GetConsumers(req, res) {
return new Promise(function (resolve, reject) {
    con.connect(function (err) {
          if (err){
              reject(err);
          }

        con.query("SELECT * FROM " + config.DATABASE + ".Contracts", 
        function (err, result, fields) {
            if (err){
              reject(err);
            }
            //console.log(result);
            resolve(result);
        });
    });
})

} }

Call GetConsumers function where ever you want it. 在任何需要的地方调用GetConsumers函数。

GetConsumers(req,res).then(rows => res.send(rows))
}).catch(err =>{ 
   console.log("Handle your error here");
   res.send("error")
})

Mysql npm has good documentation of how to use the module. Mysql npm拥有有关如何使用该模块的良好文档。 You can refer it more here 你可以在这里参考更多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每个MySQL语句是否需要一个新的查询对象? - Do I need a new query object for each MySQL statement? 我是否需要在SQL语句中添加引号? - Do I need to add a quote to my SQL statement? 为什么在这个MySQL场景下,我需要在delete语句之前添加一个无用的insert语句来防止死锁,有没有更好的方法? - Why do I need a useless insert statement before a delete statement to prevent a deadlock in this MySQL scenario, and is there a better way? 如何使带有“ IN”语句的MySQL Qyery更快? - How do I make my MySQL Qyery with an “IN” statement faster? 我是否需要使用我的pyinstaller应用程序打包MySQL服务器/客户端? - Do I need to package a MySQL server/client with my pyinstaller app? 我是否需要更改MySQL数据库的默认字符集? - Do I need to change the default character set of my MySQL database? 我的MYSQL查询中需要在哪里使用反引号或引号? - Where do I need to use backticks or quotes in my MYSQL query? 当我的机器中已经有 MySQL 时,我还需要在 Django 虚拟环境中安装 MySQL 吗? - Do I still need to install MySQL in Django virtual env while I already have MySQL in my machine? 在什么情况下需要在PHP的Mysql语句中单引号引用变量? - Under what conditions do I need to single quote a variable in a Mysql statement in PHP? 我只需要 mySql 表中唯一电话号码的计数。 我怎么做? - I need just the count of unique phone numbers in my mySql table. How do I do that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM