无法使用 process.env 连接到数据库 |乏味 |节点
Cant connect to database using process.env | Tedious | nodejs
在繁琐的配置中,使用 process.env.(数据库详细信息) 失败。但是,如果我手动输入相同的值,连接就会成功。
我想使用 process.env 而不是硬编码我的数据库详细信息。
数据库托管在 Azure 上。
这是我的代码:
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,
};
终端输出:
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 文件:
NODE_ENV=development
databaseUser=dataReader
databasePassword=***
databaseUrl=***.database.windows.net
databaseName=website
WRITE_FAILED_QUERY_TO_FILE=true
用包含相同信息的硬编码字符串替换 process.env 变量成功,returns 预期输出。
我把 process.env.(databasedata) 放在引号里了!
process.env 是一个对象。
删除引号,一切正常。
@MaazSyedAdeeb。泰。这样的盲目错误。有效。
在繁琐的配置中,使用 process.env.(数据库详细信息) 失败。但是,如果我手动输入相同的值,连接就会成功。 我想使用 process.env 而不是硬编码我的数据库详细信息。
数据库托管在 Azure 上。
这是我的代码:
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,
};
终端输出:
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 文件:
NODE_ENV=development
databaseUser=dataReader
databasePassword=***
databaseUrl=***.database.windows.net
databaseName=website
WRITE_FAILED_QUERY_TO_FILE=true
用包含相同信息的硬编码字符串替换 process.env 变量成功,returns 预期输出。
我把 process.env.(databasedata) 放在引号里了! process.env 是一个对象。 删除引号,一切正常。
@MaazSyedAdeeb。泰。这样的盲目错误。有效。