我正在执行以下脚本cmd.exe
:
acl.js
node acl.js
buckets.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()
当我运行'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;
时,我收到以下错误:
node acl.js
错误来自> 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)
文件中的const knex = require('../src/config/db');
行。
我的acl.js
文件如下所示:
db.js
正如您所看到的,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
语句在上面显示的输出中主要回复console.log
。
undefined
正在从knexfile.js
knexfile.js
.env file
.ENV
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,
},
},
}
当我运行# Database
DB_CONNECTION=postgresql
DB_ENV=development
DB_USERNAME=root
DB_PASSWORD=root
DB_DATABASE=nodeacl
或knex migrate:latest
时,种子和迁移文件运行完美。
为什么我收到上述错误的任何建议?
答案 0 :(得分:3)
当您运行knex migrate:latest
和knex seed:run
时,它会起作用,因为您的文件knexfile.js
与文件系统中的文件.env
位于同一级别。这意味着您的环境变量已正确导入。
您的文件acl.js
是否处于同一级别?如果没有,也许您应该从node /path/to/acl.js
级别运行.env
。