所以我的应用程序在我的本地运行完美,但是当我部署到 Heroku 时,生成我的分数的 .map 函数不工作,本地一切正常。我的 package.json 中有一个代理来解决 COR 问题,我还尝试更改端点以匹配我的 heroku url。来 self 的 GET 请求的数据是一个数组,因此 .map 可以正常工作。
我很确定这是一个端点问题,但我不确定如何找到错误/解决错误。任何帮助将不胜感激!
这是用来获取分数的函数
getUserScores = () => {
let id = this.state.currentUser._id
axios.get(`/api/scores/` + id)
.then(res => {
this.setState({
...this.state,
isLoaded: true,
scores: res.data
}, () => console.log(this.state.scores))
this.getHandicap();
})
.catch(err => console.log(`Something failed: ${err.message}`))
}
这是我在控制台中遇到的错误
这是 heroku 上的部署链接:https://shielded-lowlands-75979.herokuapp.com/
require("dotenv").config();
const express = require('express');
const mongoose = require('mongoose');
const config = require('config');
const cors = require('cors')
const path = require('path');
// all routes
const scores = require('./routes/api/scores');
const user = require('./routes/api/users');
const auth = require('./routes/api/auth');
const app = express();
const PORT = process.env.PORT || 5000
//bodyparser
app.use(express.json());
//Serve static assets if in production
if (process.env.NODE_ENV === 'production') {
//set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "public", "index.html"))
});
}
//mongodb connection
mongoose.connect(process.env.MONGODB_URI || config.get('mongoURI'),
{
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log("MongoDB connected..."));
//cors setup
app.use(cors());
//use routes
app.use('/api/scores', scores)
app.use('/api/users', user)
app.use('/api/auth', auth)
app.listen(process.env.PORT || PORT, () => {
console.log(`Server started on PORT ${PORT}`);
})
最佳答案
您可能已经明白了这一点,但对于遇到此问题的任何人,我会澄清其他两个问题的含义。
if (process.env.NODE_ENV === "production") {
app.use(express.static('client/build')) // set static folder
app.get('*', (req, res)=> {
res.sendFile(path.resolve(__dirname, 'client', 'build',
'index.html' ));
})
app.get('*', (req, res)=> {
res.sendFile(path.resolve(__dirname, 'client', 'build',
'index.html' ));
})
}
以上内容应该在后端/根文件夹中的 index/server.js 文件管理器中,朝向 app.listen(process.env.PORT || 5000....
的末尾app.use("/api", routes)
或类似的代码。当然,static 和 get 函数中的路径应与您的特定文件夹结构相匹配。
关于javascript - .map 在 Heroku 上部署时不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62942811/