因此,我试图设置此表单页面,正在发送信息,但是没有自动创建数据库,因此,我的信息未保存 抱歉,很长的帖子,谢谢您的反馈
这是我的app.js
mongoose.connect('mongodb://localhost/cloud', {
useNewUrlParser: true
})
})
.catch(err => console.log(err));
require('./models/post');
const post = mongoose.model('post');
const app = express();
app.post('/cloud', (req, res) => {
upload(req, res, (err) => {
if (err) {
res.render('index', {
msg: err
});
} else {
console.log(req.body);
const newUser = {
title: req.body.title,
}
new post(newUser).save().catch(post => {
res.redirect('/cloud');
})
}
});
});
app.get('/cloud', (req, res) => {
post.find({})
.sort({Date: 'desc'})
.then(posts => {
res.render('cloud/cloud', {
posts: posts
});
});
});
const port = 3000;
app.listen(port, () => {
console.log(`running ${port}`);
});
架构
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const postSchema = new Schema ({
title:{
type: String,
required: true
}
});
mongoose.model('post', postSchema);
车把文件
{{#each posts}}
<h4>{{title}}</h4>
{{else}}
<p>No pics found</p>
{{/each}}
答案 0 :(得分:0)
您没有在代码的这一行指定架构属性。
new post(newUser).save().catch(post => {
res.redirect('/cloud');
The correct way is
new post({title: new User} ).save().catch(post => {
res.redirect('/cloud');
现在,您已指定猫鼬来保存标题的新记录。