【问题标题】:.map is not a function when deploying on Heroku.map 在 Heroku 上部署时不是一个函数
【发布时间】:2020-11-06 14:25:44
【问题描述】:

所以我的应用程序在本地运行良好,但是当我部署到 Heroku 时,生成我的分数的 .map 函数不起作用,本地一切正常。我的 package.json 中有一个代理来解决 CORs 问题,我还尝试更改端点以匹配我的 heroku url。来自我的 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}`);
})

【问题讨论】:

  • 如何登录您的应用程序?请提供更多详细信息
  • 所以注册也不起作用,这就是为什么我认为这可能是路径问题。但是您可以使用 test@test.com 作为电子邮件和 1234 作为密码登录
  • 它仍然会在用户注册时将其发布到数据库,但不会重定向他们,因为它没有收到令牌...
  • 我也面临着确切的错误。有什么补救措施吗?

标签: javascript node.js reactjs heroku endpoint


【解决方案1】:

尝试将 //Serve static assets if in production 部分放在 //使用路由之后

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
【解决方案2】:

可能scores 不是一个数组,或者出于某种原因它只是undefined

【讨论】:

  • 当我在 heroku 上部署它时未定义,我猜 axios url 路径与我在本地处理它时的工作方式不同。不确定我的客户端 package.json 中的“代理”:“localhost:5000”在部署在 heroku 上时是否正常工作?
  • Heroku 可能正在使用 3000 端口,但您应该使用 process.env.PORT
  • 我在 process.env.PORT 上运行我已经添加了我的服务器文件
猜你喜欢
  • 2022-10-17
  • 1970-01-01
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-31
相关资源
最近更新 更多