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

时间:2018-11-28 17:48:49

标签: javascript node.js html5 mongodb bootstrap-4

因此,我一直在尝试从表单中获取数据并将其输入到数据库中已有一段时间,但它一直无法正常工作。没有错误,没有任何日志记录。

这是我的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>

这是我的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');
    });
});

我不知道怎么了。我的数据没有上传到数据库,由于某种原因,没有console.log()语句被执行。

2 个答案:

答案 0 :(得分:0)

承诺可能会在调用注册处理程序之前解决。

尝试以下操作:

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)
});

答案 1 :(得分:0)

具体看您的问题,您的表单中没有使用任何action

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

因此它不会发布到您的路线。 (这就是为什么我特别询问在dbconn之前是否进行控制台日志记录)