简体   繁体   English

无法使用Node.js将表单数据发送到MongoDB数据库

[英]Unable to send form data into MongoDB database using Node.js

So I've been trying to get the data from my form and input it into my database for ages and it just isn't working. 因此,我一直在尝试从表单中获取数据,并将其输入到数据库中已有很长时间了,但它一直无法正常工作。 There are no errors and nothing is logging. 没有错误,没有任何日志记录。

This is my HTML code: 这是我的HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta charset="UTF-8">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <title>Signup for NGOs</title>
</head>
<body>
    <form class="mt-4 mr-4 ml-4" id="form" method="post">
        <div class="form-group">
            <label for="name">Name</label>
            <input type="name" class="form-control" id="name" aria-describedby="emailHelp" placeholder="Enter name of NGO">
        </div>
        <div class="form-group">
            <label for="address">Address</label>
            <input type="text" class="form-control" id="address" placeholder="Address">
        </div>
        <div class="form-group">
            <label for="phone">Contact Information</label>
            <input type="number" class="form-control" id="phone" placeholder="Contact Number">
        </div>
        <div class="form-group">
            <label for="requirements">Requirements (Seperate by Commas)</label>
            <input type="text" class="form-control" id="requirements" placeholder="Requirements">
        </div>
        <button type="submit" class="btn btn-primary" id="submitButton">Submit</button>
    </form>

    <!-- Optional JavaScript -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script type="text/javascript" src="signup.js"></script>
</body>
</html>

And this is my Node.js code: 这是我的Node.js代码:

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;

var app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.resolve(__dirname, 'public')));

// I've replaced my original username and password with placeholders
var uri = "mongodb+srv://username:password@helpbook-rlpsi.gcp.mongodb.net/test?retryWrites=true";

var dbConn = MongoClient.connect(uri, { useNewUrlParser: true });
app.post('/signup', function (req, res) {
    dbConn.then(function(db) {
        delete req.body._id; // for safety reasons
        dbConn.db("NGOs").collections("partners").insertOne(req.body);
        console.log('test');
    });
});

I don't know what's going wrong. 我不知道怎么了。 My data isn't being uploaded to the database and for some reason none of the console.log() statements are being executed. 我的数据没有上传到数据库,由于某种原因,没有console.log()语句被执行。

The promise probably resolves before the signup handler is called. 承诺可能在调用注册处理程序之前解析。

Try the following: 请尝试以下操作:

var dbConn = MongoClient.connect(uri, { useNewUrlParser: true });
dbConn.then(function(client) {
    app.post('/signup', function (req, res) {
        delete req.body._id; // for safety reasons
        client.db("NGOs").collections("partners").insertOne(req.body);
        console.log('test');
    });
})
.catch(function(err){
    console.log(err)
});

Looking at your question specifically, you are not using any action in your form. 具体来看您的问题,您没有在表单中使用任何action

<form class="mt-4 mr-4 ml-4" id="form" method="post" action="/signup">
...
</form>

So it is not getting posted to your route. 因此它不会发布到您的路线。 (That's why I specifically asked if console logging before the dbconn is working or not) (这就是为什么我特别询问在dbconn之前是否进行控制台日志记录的原因)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM