我正在使用ExpressJS,MongoDB和QuillJS创建RTF博客。 我正在尝试传递Delta(QuillJS JSON对象通过Ajax发送到服务器)
$('#submit').click(function() {
var delta = quill.getContents();
console.log(delta)
$.ajax({
url: 'http://localhost:3000/',
type: 'POST',
data: {
delta: delta
},
dataType: "json",
contentType: "application/json"
})
})
这是我的服务器
articleRouter.post('/new', async function(req, res) {
let article = new Article({
type: 'blog',
title: req.body.title,
hook: req.body.hook,
content: req.body.delta,
writer: req.body.writer
})
try {
article = await article.save()
res.redirect(`read/${article.slug}`)
} catch (e) {
console.log(e)
res.render('index', { page: 'new', article: article, title: 'Create a new article' })
}
})
我得到这个错误
Error [ValidationError]: Article validation failed: content: Path `content` is required.
我相信AJAX无法正常运作,或者我错过了以下地方:
内容:req.body.delta, 我怎样才能解决这个问题?
Are you using mongoose to connect to MongoDB? If so, check your
Article
Schema. Maybecontent
is a required field.