Javascript 试图用mongoose插入单个文档,但正在插入多个文档
我目前正在Udemy学习,但无法通过他们的论坛获得答案。我正在创建自己的网站,试图更好地理解mongoDB,使用mongoose、express和EJS。该网站有多种形式。当提交带有post请求的表单数据时,我试图将一条记录插入数据库,但却插入了3或4条重复记录。当使用console.log检查发送的对象时,它也会出现几次,但是我只尝试发送一次 以下是我的app.js代码:Javascript 试图用mongoose插入单个文档,但正在插入多个文档,javascript,express,mongoose,Javascript,Express,Mongoose,我目前正在Udemy学习,但无法通过他们的论坛获得答案。我正在创建自己的网站,试图更好地理解mongoDB,使用mongoose、express和EJS。该网站有多种形式。当提交带有post请求的表单数据时,我试图将一条记录插入数据库,但却插入了3或4条重复记录。当使用console.log检查发送的对象时,它也会出现几次,但是我只尝试发送一次 以下是我的app.js代码: const express = require("express"); const bodyParse
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.set('view engine', 'ejs');
app.use(express.static("public"));
// Connect / Create to the Database
mongoose.connect("mongodb://localhost:27017/stationDB", { useNewUrlParser: true, useUnifiedTopology: true });
// Create a new Schema
const stnSchema = new mongoose.Schema ({
name: String
});
const contactSchema = new mongoose.Schema ({
name: String,
email: String,
message: String
});
// Create a new Model
const Station = mongoose.model("Station", stnSchema);
const Message = mongoose.model("Message", contactSchema);
//Creating an array of items to populate the database
const item1 = {
name: "item 1"
};
const item2 = {
name: "item 2"
};
const item3 = {
name: "item 3"
};
const item4 = {
name: "item 4"
};
const stationData = [item1, item2, item3, item4];
//Check the database to see if items have already been inserted, if not then insert the initial items
Station.find({}, function(err, results) {
if (err) {
console.log(err);
}
else {
if (results.length === 0) {
Station.insertMany(stationData), function(err) {
if (err) {
console.log(err);
}
else {
console.log("No records found - adding initial records")
}
}
}
else {
console.log("Records already in place - none added")
}
}
});
//Routes
// Get Requests
app.get("/", function(req, res) {
Station.find({}, function(err, results) {
if (err) {
console.log("Error: cannot find records for stationList");
}
else {
const stationList = results;
res.render("index", {stationItems: stationList});
}
})
});
// Post Requests
app.post("/ticketSales", function(req, res) {
const formSubmit = req.body;
const adultCost = parseFloat(formSubmit.adults) * 7.85;
const childCost = parseFloat(formSubmit.child) * 3.50;
const totalCost = adultCost + childCost;
console.log(totalCost);
res.render("ticketSales", {
adults: formSubmit.adults,
adultCost: adultCost,
children: formSubmit.child,
childCost: childCost,
ticketTotal: totalCost,
})
});
app.post("/contact", function(req, res) {
const formName = req.body.name;
const formEmail = req.body.email;
const formMsg = req.body.message;
const message = new Message({
name: formName,
email: formEmail,
message: formMsg
});
console.log(message);
message.save();
res.render("contact");
});
// App Listen
app.listen(3000, function() {
console.log('Server started, port 3000'); //
});
联系人表单的console.log()输出:
$ nodemon app.js
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
Server started, port 3000
Records already in place - none added
{
_id: 5f58a8dad8fe413a204649c7,
name: 'hobbes',
email: 'hobbes@gmail.com',
message: 'messgae\r\n '
}
{
_id: 5f58a8dad8fe413a204649c8,
name: 'hobbes',
email: 'hobbes@gmail.com',
message: 'messgae\r\n '
}
{
_id: 5f58a8dad8fe413a204649c9,
name: 'hobbes',
email: 'hobbes@gmail.com',
message: 'messgae\r\n '
}
{
_id: 5f58a8dad8fe413a204649ca,
name: 'hobbes',
email: 'hobbes@gmail.com',
message: 'messgae\r\n '
}
票证的输出:
[nodemon] starting `node app.js`
Server started, port 3000
Records already in place - none added
7.85
7.85
7.85
7.85
我对这一点还很陌生,但如果有任何帮助,我将不胜感激。我所取得的唯一进展是注意到,用于填充表单下拉列表的初始记录在插入/检索时不会重复,但通过post请求发送的记录会生效。我相信这与职位要求有关
编辑:
以下是html:
<form name="contact-form" action="/contact" method="post">
<label for="contact-name">Name</label>
<input id="contact-name" name="name" type="text">
<label for="email">Email</label>
<input id="email" name="email" type="text">
<label for="contact-msg">Message</label>
<textarea name="message" id="contact-msg">
</textarea>
<button type="submit" value="contact" name="contactBtn">Submit</button>
</form>
名称
电子邮件
消息
提交
此外,我发现将表单提交按钮从更改为将提交次数从4次减少到2次表单提交到
/contacts
的准确程度如何?这是一个基本的HTML表单吗?是的,只是简单的HTML我想你的代码被提交了多次。请出示表单代码添加HTML代码通过尝试和错误,我发现罪魁祸首是表单的文本区域。删除此选项可以解决多次提交的问题。我已经对照在线示例检查了语法,所有这些看起来都不错,但不管怎样,textarea都会导致问题。表单提交到/contacts
的准确程度如何?这是一个基本的HTML表单吗?是的,只是简单的HTML我想你的代码被提交了多次。请出示表单代码添加HTML代码通过尝试和错误,我发现罪魁祸首是表单的文本区域。删除此选项可以解决多次提交的问题。我已经对照在线示例检查了语法,看起来都不错,但不管怎样,textarea都会导致问题。