我已经完成了一个Web应用程序,并且可以在本地主机上顺利运行。当我部署到Heroku时,除了我在数据库中创建的一对多关联之外,所有内容都可以正确构建。当我在本地主机上访问我的api时,这就是项目显示的方式(这就是我想要发生的事情)
items: [
{
id: 7,
name: "A bag of chips",
image: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT5rKTzjPCyEAY8r2bj2KfOj619bmSmyApH5B9Vd716iX-To1Zg",
price: 0,
completedBid: false,
upForAuction: false,
created_at: "2019-01-04T19:46:58.301Z",
updated_at: "2019-01-07T05:50:26.561Z",
user_id: 7,
user: {
id: 7,
username: "User 6",
balance: 200,
created_at: "2019-01-04T19:46:58.239Z",
updated_at: "2019-01-07T05:50:26.569Z"
}
},
这是它在我的heroku API(没有关联)上的显示方式:
items: [
{
id: 7,
name: "A bag of chips",
image: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT5rKTzjPCyEAY8r2bj2KfOj619bmSmyApH5B9Vd716iX-To1Zg",
price: 0,
completedBid: false,
upForAuction: false,
createdAt: "2019-01-07T18:28:02.954Z",
updatedAt: "2019-01-07T18:28:02.954Z",
userId: null,
user: null
},
我在这里发现了一些类似的问题,并尝试将我的heroku数据库重置为无效。我还注意到,像在我的本地计算机上一样,userId并不突出。这两个项目的唯一区别是我在我的models / index.js文件中设置数据库的方式,因此我在下面包括了这两个文件之间的区别。任何帮助协会开展Heroku活动的帮助将不胜感激!要查看更多代码,请参阅此项目的部署分支存储库 https://github.com/7evam/liveauction/tree/deployment-test
来自models / index.js
// this line only used on heroku deployment
// require('dotenv').config();
const Sequelize = require('sequelize');
// this is how i set up my db on my local machine
const db = new Sequelize({
database: 'live_auction',
dialect: 'postgres',
define: {
underscored: true,
returning: true,
},
});
// and this is how i set it up for heroku
const db = new Sequelize(
process.env.DATABASE_URL,
{
underscored: true,
returning: true,
},
);