[英]Node sequelize throwing undefined error when checking if an object exists
好的,这要么很奇怪,要么我不了解正在发生的事情。 我正在尝试在节点中加载sequelize库。
当尝试连接时,我正在使用CLI生成的index.js文件,但以下行:
if (config.use_env_variable) {
console.log('i ran');
var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
var sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, {
dialect:config.db.dialect});
}
给我这个错误:
Cannot read property 'use_env_variable' of undefined
据我所知,该行旨在查看它是否还返回任何内容,所以我不明白为什么这会引发该错误?
更新
config在其上方的一行中被调用,到目前为止,整个文件为:
'use strict';
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(__filename);
const config = require(path.join(__dirname,'../config/config.js'));
const db = {};
console.log(config);
if (config.use_env_variable) {
console.log('i ran');
var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
var sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, {
dialect:config.db.dialect});
}
UPDATE在工作版本上添加了配置的console.log
您似乎没有config/config.json
文件,或者路径不正确。 在model/index.js
,您应该有这行
let config = require(`${__dirname}/../../config/config.json`)[env];
或类似的东西。 确保此路径是正确的路径
经过3天的错误努力,今天我找到了解决方案。
我该如何解决?
我使用JS trim()函数删除空格。
例如
process.env.NODE_ENV === "development" // return false
why ?
"development " === "development" // return false because of extra space
process.env.NODE_ENV。 trim() ===“开发” //返回true
这是我的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.