由于我的应用程序已部署到heroku,因此我收到内部服务器错误。在本地所有人都工作正常。我已经来过这里好几天了,所以我决定与社区联系!!!!
我尝试过改变表达和反应的路径,但仍然无济于事。
这是我的快递和路线文件
const express = require("express");
const mongo_uri = require("./config/db");
const path = require("path");
const bodyParser = require("body-parser");
const MongoClient = require("mongodb").MongoClient;
const helmet = require("helmet");
const cors = require("cors");
const backingRouter = require("./routes/backingRouter");
const chordsRouter = require("./routes/chordsRouter");
const arpeggiosRouter = require("./routes/arpeggiosRouter");
const app = express();
app.use(helmet());
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Handle CORS
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "*");
if (req.method === "OPTIONS") {
res.header("Access-Control-Allow-Methods", "PUT, POST, PATCH, DELETE, GET");
return res.status(200).json({});
}
next();
});
app.use("/api/backing-tracks", backingRouter);
app.use("/api/chords", chordsRouter);
app.use("/api/arpeggios", arpeggiosRouter);
// Production mode
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve((__dirname, "client", "build", "index.html")));
});
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server Started`));
MongoClient.connect(mongo_uri, { useNewUrlParser: true }).then(client => {
const db = client.db("backingTrackData");
const collection = db.collection("backings");
app.locals.collection = collection;
console.log("MongoDb running...");
});
const express = require("express");
const router = express.Router();
router.get("/", (req, res, next) => {
const { collection } = req.app.locals;
collection
.find({ tonality: { $in: ["major", "minor"] } })
.toArray()
.then(response => res.status(200).json(response))
.catch(error => res.status(500).json(error));
});
module.exports = router;
这是一个反应文件
import React, { Component } from "react";
import axios from "axios";
import SelectTonality from "./SelectTonality";
import SelectTempo from "./SelectTempo";
import Dropdown from "./Dropdown";
import ReactAudioPlayer from "react-audio-player";
class backingTracks extends Component {
state = {
backingTracksMaj: [],
backingTracksMin: [],
toggleMenu: false,
selectedBtn: "major",
selectedTempo: "90",
selectedTrack: "",
selectedTrackName: ""
};
componentDidMount() {
axios
.get("/api/backing-tracks")
.then(res => {
const backingTracksMaj = res.data
.filter(track => track.tonality === "major")
.map(newTrack => {
return newTrack;
});
const backingTracksMin = res.data
.filter(track => track.tonality === "minor")
.map(newTrack => {
return newTrack;
});
this.setState({ backingTracksMaj, backingTracksMin });
})
.catch(error => console.log(error));
}
主页加载正常,但是当我导航到backingtracks页面或任何其他页面时,我得到: GET https://jazz-guitar-woodshed.herokuapp.com/api/backing-tracks 500(内部服务器错误)
在此先感谢您的帮助!
编辑: Heroku错误
2019-05-22T23:34:23.840908+00:00 app[web.1]: TypeError: Cannot read property 'find' of undefined
2019-05-22T23:34:23.840921+00:00 app[web.1]: at router.get (/app/routes/backingRouter.js:8:6)
2019-05-22T23:34:23.840924+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2019-05-22T23:34:23.840926+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:137:13)
2019-05-22T23:34:23.840927+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2019-05-22T23:34:23.840929+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2019-05-22T23:34:23.840931+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:281:22
2019-05-22T23:34:23.840933+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)
2019-05-22T23:34:23.840938+00:00 app[web.1]: at router (/app/node_modules/express/lib/router/index.js:47:12)
2019-05-23T00:08:42.152915+00:00 heroku[web.1]: Idling
2019-05-23T00:08:42.157365+00:00 heroku[web.1]: State changed from up to down
2019-05-23T00:08:43.157938+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2019-05-23T00:08:43.269315+00:00 heroku[web.1]: Process exited with status 143
2019-05-22T23:34:23.840935+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:275:10)
答案 0 :(得分:0)
也许代替这个
app.use(bodyParser.urlencoded({ extended: false }));
尝试一下:
app.use(express.json());
app.use(express.urlencoded({extended: true}));
将设置扩展为true允许嵌套的JSON对象通过express发送,而对象显然就是。香草快递也将执行bodyParser的工作。因此,您实际上并不需要它。
答案 1 :(得分:0)
我终于明白了。它必须与我的mongodb白名单一起使用。我只有IP地址,这是访问dB的唯一方法。谢谢您的宝贵意见!