最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Nodejs有MYSQL问题,无法在连接方法之外进行查询

IT培训 admin 5浏览 0评论

Nodejs有MYSQL问题,无法在连接方法之外进行查询

我制作了一次连接到mysql服务器的函数,它工作正常。现在我做其他功能只是查询,但我不能这样做。我在我的app.js中调用此函数

进口:

const mysql = require("mysql");
const config = require("../configuration/config");

const connectToDB = () => {
  const connection = mysql.createConnection(config.database);
  connection.connect(err => {
    if (err) return console.log(err);
  });
  console.log("MYSQL DB CONNECTED");
};

然后我执行此功能

const getAllUserReports = userID => {
  return new Promise((resolve, reject) => {
    const reports = mysql.query(
      `SELECT reports.id,report_types.[type],companies.id as companyID,reports.[year]
      FROM reports
      INNER JOIN report_types on reports.reportTypeID=report_types.id
      INNER JOIN companies on reportspanyID=companies.id
      where reports.creatorUserID=${userID} and active_report=1
      `
    );
    resolve(reports);
    reject("Server Error");
  });
};

我在路由中调用了第二个函数,但“ mysql.query”不起作用。在sql server中,其工作是这样的。

还有其他方法可以做到?查询一次后,我不想连接。服务器打开时,我想要一个连接。

回答如下:

使用mysql npm package时,无法从mysql对象运行查询。您必须改为从connection对象运行它。您必须说

const reports = connection.query(/*whatever*/, function(error, results, fields) {
    /*handle query results*/
})

或承诺的等价物。否则,mysql软件包不知道要使用哪个连接。

并且,就算您的服务器保持运行一段时间,您一次打开连接并重新使用它的策略是将失败]。如果连接断开,您的服务器将无法继续。

使用connection pool。然后从池中为必须运行的每个查询获取连接。这样就将连接管理放到了健壮且经过调试的代码段中。而且,您还将获得并发异步查询。

类似这样的事情(我尚未调试)可能有用。

const mysql = require("mysql");
const config = require("../configuration/config");
const pool mysql.createPool(config.databaseConnectionPool);

...

let resultset;
pool.query (/*whatever*/, function (error, result, fields) {
   if (error) throw error;
   resultset = results;
});

Nodejs有MYSQL问题,无法在连接方法之外进行查询

我制作了一次连接到mysql服务器的函数,它工作正常。现在我做其他功能只是查询,但我不能这样做。我在我的app.js中调用此函数

进口:

const mysql = require("mysql");
const config = require("../configuration/config");

const connectToDB = () => {
  const connection = mysql.createConnection(config.database);
  connection.connect(err => {
    if (err) return console.log(err);
  });
  console.log("MYSQL DB CONNECTED");
};

然后我执行此功能

const getAllUserReports = userID => {
  return new Promise((resolve, reject) => {
    const reports = mysql.query(
      `SELECT reports.id,report_types.[type],companies.id as companyID,reports.[year]
      FROM reports
      INNER JOIN report_types on reports.reportTypeID=report_types.id
      INNER JOIN companies on reportspanyID=companies.id
      where reports.creatorUserID=${userID} and active_report=1
      `
    );
    resolve(reports);
    reject("Server Error");
  });
};

我在路由中调用了第二个函数,但“ mysql.query”不起作用。在sql server中,其工作是这样的。

还有其他方法可以做到?查询一次后,我不想连接。服务器打开时,我想要一个连接。

回答如下:

使用mysql npm package时,无法从mysql对象运行查询。您必须改为从connection对象运行它。您必须说

const reports = connection.query(/*whatever*/, function(error, results, fields) {
    /*handle query results*/
})

或承诺的等价物。否则,mysql软件包不知道要使用哪个连接。

并且,就算您的服务器保持运行一段时间,您一次打开连接并重新使用它的策略是将失败]。如果连接断开,您的服务器将无法继续。

使用connection pool。然后从池中为必须运行的每个查询获取连接。这样就将连接管理放到了健壮且经过调试的代码段中。而且,您还将获得并发异步查询。

类似这样的事情(我尚未调试)可能有用。

const mysql = require("mysql");
const config = require("../configuration/config");
const pool mysql.createPool(config.databaseConnectionPool);

...

let resultset;
pool.query (/*whatever*/, function (error, result, fields) {
   if (error) throw error;
   resultset = results;
});
发布评论

评论列表 (0)

  1. 暂无评论