尝试使用nodejs中的mongoose将表单数据提交到我的数据库,但是当我在postman上运行代码时,它工作正常但不在浏览器中..并且没有数据保存到数据库中...我无法弄清楚为什么......请帮忙吗?
<div class="card bg-primary text-center card-form ">
<div class="card-body">
<h3>sign up </h3>
<p>fill up the form below to register</p>
<form action="/users" method="POST">
<div class="form-group">
</div>
<div class="form-group">
<input class="form-control form-control-lg" name="username" type="text" placeholder="Username">
<input class="form-control form-control-lg" name="firstname" type="text" placeholder="FirstName">
</div>
<div class="form-group">
<input class="form-control form-control-lg" name="lastname" type="text" placeholder="LastName">
</div>
<div class="form-group">
<input type="submit" value="submit" class="btn btn-outline-light btn-block">
</div>
</form>
</div>
</div>
var express = require("express");
var mongoose = require("mongoose");
mongoose.Promise = global.Promise
var bodyParser = require("body-parser");
var MongoClient = require("mongodb").MongoClient;
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended : true }));
mongoose.connect("mongodb://localhost:27017/register");
var mySchema = new mongoose.Schema({
userName : { type: String, required: true},
firstName : {type: String, required: true},
lastName : {type: String, required: true}
});
var users = mongoose.model("users", mySchema);
app.get("/", (req, res)=>{
res.sendFile(__dirname + "/index.html");
});
app.post("/users", (req, res)=>{
var myData = new users(req.body);
myData.save().then(item =>{
res.send(" your data has been saved in our database ");
}).catch(err =>{
res.status(400).send("sorry, we are unable to save your data")
});
} );
app.listen(3000, ()=>{
console.log("we are listenning on port 3000")
})