如何使用猫鼬将其正确保存到mongoDB集合中?

希望一切都好!我是新来的,我有一个问题,我使用Mongoose,Express将这种post方法发布到mongoDB时遇到了这个问题,它没有进入集合,但是我正确连接到数据库,因为我看到MongoDB数据库连接已成功建立。终端,但我在应用程序的控制台中收到此404错误,其中包含来自我要放入地图集的练习集中的表格中的实际数据。这让我发疯了,因为我一直在谷歌上阅读和阅读他们的文档已有一段时间,但无法正确地连接各个部分。如果有任何帮助,将不胜感激。

   <form action="/api/form" method="POST" onSubmit={handleSubmit}>

    onSubmit={(values,actions) => {
              setTimeout(() => {
                axios
                .post('/api/form',values,actions)
                .then(response => {
                  console.log(response);
                })
                .catch(error => {
                  console.log(error.response);
                });
                console.log({ values,actions });
                alert(JSON.stringify(values,null,2));
                actions.setSubmitting(false);
              },400);
            }}

const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const Exercise = require("./models/routes.model");
require("dotenv").config();
​
const PORT = process.env.PORT || 8080;
const app = express();
​
app.use(
  bodyParser.urlencoded({
    extended: true
  })
);
​
app.use(cors());
app.use(express.json());
​
const uri = process.env.ATLAS_URI;
mongoose.connect(uri,{ useNewUrlParser: true,useCreateIndex: true });
const db = mongoose.connection;
db.on('error',(error) => console.error(error))
db.once("open",() => {
  console.log("MongoDB database connection established successfully");
});
​
app.use(require('./routes/routes'));
app.post("/api/form",async (req,res) => {
  const firstName =   req.body.firstName;
  const middleName =  req.body.middleName;
  const lastName =  req.body.lastName;
  const phone =  Number(req.body.phone);
  const email =  req.body.email;
  const confirmEmail =  req.body.confirmEmail;
  const comments =  req.body.comments;
  const Streetaddress =  req.body.Streetaddress;
  const unit =  req.body.unit;
  const state =  req.body.state;
  const city =  req.body.city;
  const zip =  req.body.zip;
  const typeOfDegree =  req.body.typeOfDegree;
  const degreeAttained =  req.body.degreeAttained;
  const educationalInstitution =  req.body.educationalInstitution;
  const otherInformation =  req.body.otherInformation;
​
  const newExercise =  new Exercise({
    firstName,middleName,lastName,phone,email,confirmEmail,comments,Streetaddress,unit,state,city,zip,typeOfDegree,degreeAttained,educationalInstitution,otherInformation
  });
  console.log(Exercises);
  newExercise.save()
    .then(() => res.json('Email added to DB!'))
    .catch(err => res.json('Error: ' + err));
});
​
app.use(bodyParser.json()); // get information from html forms
if (process.env.NODE_ENV === "production") {
  // Exprees will serve up production assets
  app.use(express.static("/app/index.html"));
​
  // Express serve up index.html file if it doesn't recognize route
  const path = require("path");
  app.get("*",(req,res) => {
    res.sendFile(path.resolve("/app/index.html"));
  });
}
​
// Listen to whatever port above.
app.listen(PORT,() => {
  console.log(`Our app is running on port ${PORT}`);
});
Collapse

sonnysong 回答:如何使用猫鼬将其正确保存到mongoDB集合中?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3091609.html

大家都在问