gpt4 book ai didi

node.js - 引用错误 : DATABASE_URL is not defined when trying to connect to heroku postgres db using Sequelize

转载 作者:行者123 更新时间:2023-12-03 22:31:11 24 4
gpt4 key购买 nike

我正在尝试在 Heroku 上使用 Sequelize 托管我的 Node.js API。我收到未定义 DATABASE_URL 的引用错误。我创建了一个附加的 postgres 数据库,并在显示配置变量时在那里看到它。
我的 config.js 文件:

require('dotenv').config();
module.exports = {
development: {
database: process.env.DATABASE,
dialect: "postgres",
password: process.env.DATABASE_PASSWORD
},
test: {
database: process.env.DATABASE,
dialect: "postgres"
},
production: {
use_env_variable: DATABASE_URL,
dialect: "postgres",
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false
}
}
}
}
我的模型/index.js 文件:
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.js')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

// attach models
db.user = require("../models/user.js")(sequelize, Sequelize);
db.role = require("../models/role.js")(sequelize, Sequelize);
db.userRole = require("../models/userRole.js")(sequelize, Sequelize);
db.garden = require("../models/garden.js")(sequelize, Sequelize);
db.seed = require("../models/seed.js")(sequelize, Sequelize);
db.plant = require("../models/plant.js")(sequelize, Sequelize);
db.plantImage = require("../models/plantImage.js")(sequelize, Sequelize);

// list associations below
db.role.belongsToMany(db.user, {
through: "userRole",
foreignKey: "roleId",
otherKey: "userId"
});
db.user.belongsToMany(db.role, {
through: "userRole",
foreignKey: "userId",
otherKey: "roleId"
});

db.seed.belongsToMany(db.garden, {
through: "plant",
foreignKey: "seedId",
otherKey: "gardenId"
});
db.garden.belongsToMany(db.seed, {
through: "plant",
as: "planted",
foreignKey: "gardenId",
otherKey: "seedId"
});

db.user.hasMany(db.garden, {
foreignKey: 'userId'
});
db.garden.belongsTo(db.user, {
foreignKey: "userId"
});

db.user.hasMany(db.seed, {
foreignKey: "userId",
});
db.seed.belongsTo(db.user);
db.plantImage.belongsTo(db.plant);

module.exports = db;

我的 server.js 文件
const express = require("express");
const cors = require("cors");
const db = require("./models");

const app = express();
app.disable('x-powered-by');
var corsOptions = {
origin: "*",
};

app.use(cors(corsOptions));

db.sequelize.sync()

app.get("/", (req, res) => {
res.json({ message: "Welcome to Giancarlo's application." });
});

require('./routes/auth.routes')(app);
require('./routes/user.routes')(app);
require('./routes/role.routes')(app);
require("./routes/garden.routes")(app)
require("./routes/seed.routes")(app)
require("./routes/plant.routes")(app)
require("./routes/plantImage.routes")(app)

// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
我的 package.json 文件:
{
"name": "my-garden-express-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon server.js",
"start": "node server.js",
"db:reset": "npx sequelize-cli db:drop && npx sequelize-cli db:create && npx sequelize-cli db:migrate && npx sequelize-cli db:seed:all"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt-nodejs": "^0.0.3",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"pg": "^8.5.1",
"sequelize": "^6.3.5",
"dotenv": "^8.2.0"
},
"devDependencies": {
"nodemon": "^2.0.7",
"sequelize-cli": "^6.2.0"
}
}
我不明白为什么它找不到 DATABASE_URL。我查看了几个 stackoverflow 问题并进行了我所看到的更改,但仍然出现相同的错误。先谢谢您的帮助!

最佳答案

use_env_variable 键应该设置为字符串 'DATABASE_URL' 而不是变量 DATABASE_URL

{
use_env_variable: 'DATABASE_URL',
// ...
}

关于node.js - 引用错误 : DATABASE_URL is not defined when trying to connect to heroku postgres db using Sequelize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66755006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com