首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用process.env | Tedious | nodejs连接到数据库

无法使用process.env | Tedious | nodejs连接到数据库
EN

Stack Overflow用户
提问于 2020-05-02 15:20:41
回答 1查看 1.3K关注 0票数 1

在繁琐的配置中,使用process.env.(数据库详细信息)失败。但是,如果我手动输入相同的值,则连接成功。我想使用process.env而不是硬编码我的数据库详细信息。

数据库托管在azure上。

下面是我的代码:

代码语言:javascript
复制
const { Connection, Request } = require("tedious");
const dotenv = require("dotenv");

dotenv.config();

// Create connection to database
const config = {
  authentication: {
    options: {
      userName: "process.env.databaseUser",
      password: "process.env.databasePassword",
    },
    type: "default",
  },
  server: "process.env.databaseUrl",
  options: {
    database: "process.env.databaseName",
    encrypt: true,
  },
};

const connection = new Connection(config);

// Attempt to connect and execute queries if connection goes through
connection.on("connect", (err) => {
  if (err) {
    console.error(err.message);
  } else {
    queryDatabase();
  }
});

async function queryDatabase(query) {
  const request = new Request(query, (err, rowCount) => {
    if (err) {
      console.error(err.message);
    } else {
      console.log(`${rowCount} row(s) returned`);
    }
  });

  request.on("row", (columns) => {
    columns.forEach((column) => {
      console.log("%s\t%s", column.metadata.colName, column.value);
    });
  });

  connection.execSql(request);
}
module.exports = {
  queryDatabase,
};

终端输出:

代码语言:javascript
复制
Worker 09d76af8-01c2-4cdb-a2c1-d84de5ccfb89 connecting on 127.0.0.1:41165
[5/2/20 7:12:06 AM] Sat, 02 May 2020 07:12:06 GMT tedious deprecated The default value for `config.options.trustServerCertificate` will change from `true` to `false` in the next major version of `tedious`. Set the value to `true` or `false` explicitly to silence this message. at shared/my-db-helper.js:22:20
[5/2/20 7:12:06 AM] Sat, 02 May 2020 07:12:06 GMT tedious deprecated In the next major version of `tedious`, creating a new `Connection` instance will no longer establish a connection to the server automatically. Please use the new `connect` helper function or call the `.connect` method on the newly created `Connection` object to silence this message. at internal/process/next_tick.js:131:7
[5/2/20 7:12:06 AM] Failed to connect to process.env.databaseUrl:1433 - getaddrinfo ENOTFOUND process.env.databaseUrl
[5/2/20 7:12:07 AM] (node:21327) [DEP0064] DeprecationWarning: tls.createSecurePair() is deprecated. Please use tls.Socket instead.

.env文件:

代码语言:javascript
复制
NODE_ENV=development
databaseUser=dataReader
databasePassword=***
databaseUrl=***.database.windows.net
databaseName=website
WRITE_FAILED_QUERY_TO_FILE=true

用包含相同信息的硬编码字符串替换process.env变量成功,并返回预期的输出。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-02 15:27:19

我把process.env.(数据库数据)放在引号里!process.env是一个对象。去掉引号,一切都正常了。

@MaazSyedAdeeb。泰。真是个盲目的错误。它起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61555789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档