我正在创建一个以Sequelize作为ORM的Node.js / Express.js应用程序。 This is the tutorial I'm following.我无法将表迁移到我的Azure SQL数据库中。
我的config/config.json
文件是这样的:
{
"development" : {
"use_env_variable" : "DATABASE_URL",
"dialect" : "mssql"
}
}
我的
.env
文件是这样的:DATABASE_URL=<connection string here>
这些通过
models/index.js
中的代码调用:'use strict';
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(module.filename);
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.json')[env];
var db = {};
if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
var sequelize = new Sequelize(config.database, config.username, config.password, config);
}
我的主文件
require('dotenv').config()
中有app.js
。我的理解是dotenv
包会在整个项目中导出所有.env
变量。当我尝试在终端中运行
node_modules/.bin/sequelize db:migrate
时,我得到ERROR: Error parsing url: undefined
为什么返回未定义状态?
这是我的文件结构,以防万一。
最佳答案
您在哪里需要您的.env
文件?运行迁移时,不需要此文件,因此会出现错误。
您应该在require('dotenv').config();
中添加config/config.js
作为第一行。