简体   繁体   English

如何从Mysql连接的响应中获取数据

[英]How to get data from response of Mysql connection

I want to know whether there is any existing user with emailid in my db by the name say xyz@abc.com . 我想知道数据库中是否有名称为xyz@abc.com的电子邮件标识的现有用户。

function isDuplicateUser(emailId)
{
  var sql='SELECT count(*) FROM UserDetails WHERE emailId ="'+emailId+'";';
  var con = mysql.createConnection({
    host:"localhost",
    user: "root",
    password: "32577488",
    database:"mydb"
  });
  con.connect(function(err) {
    con.query(sql, function (err, result) {
      if (err) throw err;
      console.log("Inside Duplicate check");
      console.log(result[0]);
      console.log(result.count);
      console.log(result[0].emailId);
      console.log(result[0].count);
      console.log("1 record inserted");
    });
  });
}

But in all the console.log statements I am getting undefined ! 但是在所有console.log语句中,我都变得不确定! I thought of using count so that if there is more than 0 than there exists a user with that userid ! 我想到了使用count,以便如果存在大于0的用户,则存在具有该userid的用户! Please help me ! 请帮我 ! I have another doubt also It is ok to get connection in every function ! 我也有另一个疑问,可以在每个功能中建立连接! Like for signup have a 喜欢注册

    function signup(email,password,name)
    {
       var sql =''....;
       //getting connection once

    }
function signin(emailid,password)
{
    //getting connection here 
    return success;
}

It seems like code is getting replicated many times ! 似乎代码正在被复制很多次!

please, try with this sql statement and let me know the result: 请尝试使用此sql语句,并让我知道结果:

// replace your var sql with this one
const sql = `SELECT count(*) FROM UserDetails WHERE emailId = '${emailId}'`;

In the other hand, regarding how to manage connection in each function, my recommendation is create a pool of connections in your app and get a connection from the pool on each function you need (you need to take care about releasing the connection when you finish to make them available for new incoming requests): 另一方面,关于如何管理每个功能中的连接,我的建议是在您的应用中创建一个连接池,并从您需要的每个功能上从该池中获取一个连接(您需要注意在完成后释放连接)使它们可用于新的传入请求):

// connection first time when node starts
const options = {
  connectionLimit: 10,
  host: HOST,
  user: USER,
  password: PASSWORD,
  database: DATABASE,
  port: 3306,
  timezone: 'Z',
  // debug: true,
  multipleStatements: true,
  // ...azureCertificate && sslOptions,
};

const pool = mysql.createPool(options);

// on each function, get a connection from the pool using this function
getConnection() {
  return new Promise((resolve, reject) => {
    if (pool === null) {
      return reject(new Error(`MySQL connection didn't established. You must connect first.`));
    }

    pool.getConnection((err, connection) => {
      if (err) {
        if (connection) {
          connection.release();
        }

        return reject(err);
      }

      return resolve(connection);
    });
  });
}

Hope this helps. 希望这可以帮助。

This example was made using npm package mysql : https://www.npmjs.com/package/mysql 这个例子是使用npm软件包mysql制作的https ://www.npmjs.com/package/mysql

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM