无法在 POST (mongoose/express) 上创建新文档
Posted
技术标签:
【中文标题】无法在 POST (mongoose/express) 上创建新文档【英文标题】:Can't create new document on POST (mongoose/express) 【发布时间】:2021-12-29 17:36:09 【问题描述】:请检查我的 Node.js 代码我想在猫鼬罗盘中保存联系人页面数据,这不会引发任何错误,但也不会保存数据
<form action="/" class="contact_form grid" method="POST">
html 是完全正确的,我认为请告诉我这个 APP.JS 代码有什么问题
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const bodyparser = require('body-parser')
const path = require('path');
mongoose.connect('mongodb://localhost:27017/ContactPage');
const port = 3000;
var contactSchema= new mongoose.Schema(
name:type:String,
email:type:String,
project:type:String,
message:type:String
);
var Contact = mongoose.model('Contact', contactSchema);
module.exports = Contact;
app.use('/static', express.static('static')) // For serving static files
app.use(express.urlencoded(extended: true))
app.set('view engine', 'ejs');
app.get('/', function(req,res)
res.sendFile(path.join(__dirname, "index.html"))
);
app.post('/',(req,res)=>
var myData = new Contact(req.body);
myData.save().then(()=>
res.send(req.body)
).catch(()=>
res.status(400).send(req.body);
);
);
app.listen(port, () =>
console.log(`Example app listening at http://localhost:$port`)
)
请检查我的 Node.js 代码我想在猫鼬罗盘中保存联系人页面数据,这不会引发任何错误,但也不会保存数据
<form action="/" class="contact_form grid" method="POST">
<div class="contact_inputs grid">
<div class="contact_content">
<label for="" class="contact_label">Name</label>
<input type="text" name= "name" class="contact_input">
</div>
<div class="contact_content">
<label for="" class="contact_label">Email</label>
<input type="email"name="email" class="contact_input">
</div>
</div>
<div class="contact_content">
<label for="" class="contact_label">Project</label>
<input type="text" name="project" class="contact_input">
</div>
<div class="contact_content">
<label for="" class="contact_label">Message</label>
<textarea name="" id="" cols="0" rows="7" name = "message" class="contact_input"></textarea>
</div>
<div>
<a href="" class="button button--flex">
Send Message
<i class="fas fa-paper-plane button_icon"></i>
</a>
</div>
</form>
【问题讨论】:
没必要SHOUT 答案对你有用吗 【参考方案1】:#1 你不是从 HTML 提交的,所以不是:
<div>
<a href="" class="button button--flex">
Send Message
<i class="fas fa-paper-plane button_icon"></i>
</a>
</div>
写:
<button type="submit">
Send Message
</button>
#2表示改进
contactSchema 文件:
const mongoose = require('mongoose')
var contactSchema = new mongoose.Schema(
name: type: String ,
email: type: String ,
project: type: String ,
message: type: String
);
module.exports = mongoose.model('Contact', contactSchema);
应用文件:
const express = require('express');
const mongoose = require('mongoose');
const bodyparser = require('body-parser')
const path = require('path');
const port = 3000;
const app = express();
app.use('/static', express.static('static')) // For serving static files
app.use(express.urlencoded( extended: true ))
app.set('view engine', 'ejs');
app.get('/', function (req, res)
res.sendFile(path.join(__dirname, "index.html"))
);
app.post('/', (req, res) =>
var name, email, project, message = req.body;
var myData = new Contact(
name,
email,
project,
message,
);
myData.save().then((newData) =>
res.send(newData)
).catch((err) =>
res.status(400).send("Error: ", err, " , Data: ", req.body);
);
);
app.listen(port, () =>
mongoose.connect('mongodb://localhost:27017/ContactPage')
.then(() => console.log("Server & DB listening at http://localhost:$port"))
.catch(err => console.log(err));
)
【讨论】:
以上是关于无法在 POST (mongoose/express) 上创建新文档的主要内容,如果未能解决你的问题,请参考以下文章
无法从 Django request.POST 获取 POST 数据