Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用node.JS将数据输入mongodb后重定向到JS文件_Javascript_Node.js_Reactjs - Fatal编程技术网

Javascript 使用node.JS将数据输入mongodb后重定向到JS文件

Javascript 使用node.JS将数据输入mongodb后重定向到JS文件,javascript,node.js,reactjs,Javascript,Node.js,Reactjs,我正在创建一个公文包网站,其中有一个包含三个输入字段的contact.js文件。在输入字段中输入数据并单击submit按钮后,数据将存储在mongodb中,其后端位于文件db.js中。单击submit按钮后,它将被重定向到显示输入的详细信息的页面。我想被重定向到我的主页(landingPage.js) contact.js import React, { Component } from "react"; class Contact extends Component { state =

我正在创建一个公文包网站,其中有一个包含三个输入字段的
contact.js
文件。在输入字段中输入数据并单击submit按钮后,数据将存储在mongodb中,其后端位于文件
db.js
中。单击submit按钮后,它将被重定向到显示输入的详细信息的页面。我想被重定向到我的主页(
landingPage.js

contact.js

import React, { Component } from "react";

class Contact extends Component {
  state = {};

  render() {
    return (
      <div className="container">
      <div className="row bg-contact justify-content-center">
          <div className="col-6">
            <form
              className="form-style"
              method="POST"
              action="http://localhost:2000/post-feedback"
            >
                  <h1>Hola!</h1>
                  <h4>
            {" "}
            I am based out of Delhi and will be happy to assist you with
            your request
          </h4>
          <div className="form-group">
            <label htmlFor="exampleInputEmail1">Email address</label>
            <input
              type="email"
              name="email"
              className="form-control"
              id="exampleInputEmail1"
              aria-describedby="emailHelp"
              placeholder="Enter email"
            ></input>
            <small id="emailHelp" className="form-text text-muted">
              We'll never share your email with anyone else.
            </small>
          </div>
          <div className="form-group">
            <label htmlFor="exampleInputName1">Name</label>
            <input
              type="Name"
              name="Name"
              className="form-control"
              id="exampleInputName1"
              placeholder="Name"
            ></input>
          </div>
          <div className="form-group">
            <label htmlFor="exampleFormControlMessage1">Message</label>
            <textarea
              type="message"
              name="message"
              className="form-control"
              id="exampleFormControlMessage1"
              rows="3"
            ></textarea>
          </div>

          <input type="submit" className="btn btn-dark" value="Submit" />
        </form>
      </div>
      <div className="row col-12 justify-content-center padding-custom">
        <div>
          <h2>Connect</h2>
          <div className="social pa-icon">
            <a
              href="https://www.facebook.com/gokul.rajan.140"
              target="_blank"
            >
              {" "}
              <i className="fa fa-facebook"></i>
            </a>
            <a
              href="https://www.linkedin.com/in/gokul-rajan-90a368169/"
              target="_blank"
            >
              {" "}
              <i className="fa fa-linkedin"></i>
            </a>
            <a href="#">
              {" "}
              <i className="fa fa-github"></i>
            </a>
            <a href="#">
              {" "}
              <i className="fa fa-youtube"></i>
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>
    );
  }
}

export default Contact;
我已经看过了


我已经尝试了几乎所有的方法,但是在点击提交按钮后,我仍然没有被重定向到
landingPage.js

你永远不能直接从一种方法重定向到另一种方法
您只能将表单POST重定向到POST

import LandingPage from "./landingPage";
var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");

var dbConn = 
mongodb.MongoClient.connect("mongodb://localhost:27017/resume");

var app = express();
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  next();
});

app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.resolve(__dirname, 
"src/components/contact.js")));

app.post("/post-feedback", function(req, res) {
  dbConn.then(function(db) {
    delete req.body._id; // for safety reasons
    db.collection("feedbacks").insertOne(req.body);
  });
  // res.send("Data received:\n" + JSON.stringify(req.body));
  res.redirect("/landingPage"); 
});
app.get("/landingPage", function(req, res) {
  res.sendFile(__dirname + { LandingPage });
});

 app.listen(process.env.PORT || 2000, process.env.IP || "0.0.0.0");