我正在尝试将博客帖子内容保存到数据库并从中检索数据。但是当函数进入保存和查找函数时,函数停止执行。
功能如下:
blogapi.post('/add', function(req, res) {
var blog = new Blog({
title: req.body.title,
author: req.body.author,
blogDate: req.body.blogDate,
blogLink: req.body.blogLink,
shortDesc: req.body.shortDesc,
blogImage: req.body.blogImage
});
blog.save(function(err) {
if (err) {
res.send(err);
return;
}
res.json({message: 'Blog Post Added!'});
});
});
blogapi.get('/view', function(req, res) {
Blog.find({}, function(err, blogs) {
if (err) {
res.send(err);
return;
}
res.json(blogs);
});
});
Blog Schema如下:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var blogSchema = new Schema({
title: {type: String, required: true},
author: {type: String, required: true},
blogDate: {type: String, required: true},
blogLink: {type: String, required: true},
shortDesc: {type: String, required: true},
blogImage: {type: String, required: false}
});
blogSchema.pre('save', function(next){
var blogPost = this;
if (!blogPost.isModified('title')) {
return next();
}
next();
});
module.exports = mongoose.model('Blog', blogSchema);
我搜索了各种文章,发现确实我的执行虽然保存在预先停止,但我不知道为什么。我在这里听了一个问题的答案,但它没有解决我的问题。
我仍然不知道为什么找到函数正在停止。