问题
I am executing the following script, node acl.js
:
acl.js
require('dotenv').config()
const _ = require('lodash');
const buckets = require('./buckets');
const knex = require('../src/config/db'); //HERE I am getting the ERROR
var downSql = 'DROP TABLE IF EXISTS "{{prefix}}{{meta}}";'+
'DROP TABLE IF EXISTS "{{prefix}}{{resources}}";'+
'DROP TABLE IF EXISTS "{{prefix}}{{parents}}";'+
'DROP TABLE IF EXISTS "{{prefix}}{{users}}";'+
'DROP TABLE IF EXISTS "{{prefix}}{{roles}}";'+
'DROP TABLE IF EXISTS "{{prefix}}{{permissions}}";';
var upSql = 'CREATE TABLE "{{prefix}}{{meta}}" (key TEXT NOT NULL PRIMARY KEY, value TEXT[][] NOT NULL);'+
'INSERT INTO "{{prefix}}{{meta}}" VALUES (\'users\', \'{}\');'+
'INSERT INTO "{{prefix}}{{meta}}" VALUES (\'roles\', \'{}\');'+
'CREATE TABLE "{{prefix}}{{resources}}" (key TEXT NOT NULL PRIMARY KEY, value TEXT[][] NOT NULL);'+
'CREATE TABLE "{{prefix}}{{parents}}" (key TEXT NOT NULL PRIMARY KEY, value TEXT[][] NOT NULL);'+
'CREATE TABLE "{{prefix}}{{roles}}" (key TEXT NOT NULL PRIMARY KEY, value TEXT[][] NOT NULL);'+
'CREATE TABLE "{{prefix}}{{users}}" (key TEXT NOT NULL PRIMARY KEY, value TEXT[][] NOT NULL);'+
'CREATE TABLE "{{prefix}}{{permissions}}" (key TEXT NOT NULL PRIMARY KEY, value JSON NOT NULL);';
function tmpl(str, ctx) {
var n = 1;
var sql = str.replace(/{{(\w+)}}/g, function(match, cap1) {
return ctx[cap1] || match;
});
return sql.replace(/\?/g, function() { return '$' + n++; });
}
function createTables(callback) {
var prefix = ''
var bucketNames = buckets(args[8])
if (!prefix) prefix = 'acl_';
knex.raw(tmpl(downSql+upSql, {
'meta': bucketNames.meta,
'parents': bucketNames.parents,
'permissions': bucketNames.permissions,
'prefix': prefix,
'resources': bucketNames.resources,
'roles': bucketNames.roles,
'users': bucketNames.users
}))
.then(function() {
if (!_.isUndefined(callback)) {
callback(null, db);
}
})
;
}
createTables()
buckets.js
'use strict';
var _ = require('lodash');
var buckets = function(options){
return _.extend({
meta: 'meta',
parents: 'parents',
permissions: 'permissions',
resources: 'resources',
roles: 'roles',
users: 'users'
}, options);
};
exports = module.exports = buckets;
When I run node acl.js
I get the following error:
> node acl.js
{ development:
{ client: undefined,
connection: { user: undefined, password: undefined, database: undefined } } }
undefined
C:\Users\admin\Desktop\Coding Projects\learning_npm_node_acl\node_modules\knex\lib\index.js:49
if (arguments.length === 0 || !config.client && !config.dialect) {
^
TypeError: Cannot read property 'client' of undefined
at Knex (C:\Users\admin\Desktop\Coding Projects\learning_npm_node_acl\node_modules\knex\lib\index.js:49:40)
at Object.<anonymous> (C:\Users\admin\Desktop\Coding Projects\learning_npm_node_acl\src\config\db.js:8:29)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Module.require (module.js:517:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Users\admin\Desktop\Coding Projects\learning_npm_node_acl\node_acl\acl.js:5:14)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
The error comes from the line const knex = require('../src/config/db');
in my acl.js
file.
My db.js
file looks like the following:
require('dotenv').config()
// Loading from an external file
const config = require('../../knexfile')
console.log(config)
const env = process.env.DB_ENV
console.log(env)
const knex = require('knex')(config[env])
knex.on('query', (queryData) => {
console.log(queryData)
})
module.exports = knex
As you can see the console.log
statements give back mostly undefined
in the above shown output.
The knexfile.js
is loading the configuration from a .env file
knexfile.js
require("dotenv").config()
module.exports = {
development: {
client: process.env.DB_CONNECTION,
connection: {
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
},
},
}
.env
# Database
DB_CONNECTION=postgresql
DB_ENV=development
DB_USERNAME=root
DB_PASSWORD=root
DB_DATABASE=nodeacl
When I run knex migrate:latest
or knex seed:run
the seeds and migration files run perfectly.
Any suggestions why I am getting the above shown error?
回答1:
When you run knex migrate:latest
and knex seed:run
, it works because your file knexfile.js
is located at the same level as your file .env
in your file system. That means your environment variables is correctly imported.
Is your file acl.js
at the same level ? If not, maybe you should run node /path/to/acl.js
from the .env
level.
来源:https://stackoverflow.com/questions/47379839/undefined-db-connection-with-knex