为什么我这里提交请求,title和content 时undefined
来源:6-7 API对接mysql(博客详情和新建)
丛从绿草
2019-06-20
const newBlog = (blogData ={}) => {
// blogData 是个博客对象,包含title content 属性
const title = blogData.title
const content = blogData.content
const author = blogData.author
const createTime = Date.now()
const sql = `insert into blogs (title, content, createtime, author) values ('${title}', '${content}', '${createTime}', '${author}');`
return exec(sql).then(insertData => {
console.log('insertData is ', insertData)
return {
id: insertData.insertId
}
})
}
// 新建一篇博客
if (method === ‘POST’ && req.path === ‘/api/blog/new’) {
// const data = newBlog(req.body)
// return new SuccessModel(data);
req.body.author = ‘zhangsan’; // 假数据,待开发登录时再改成真实数据
const result = newBlog(req.body);
return result.then(data => {
return new SuccessModel(data);
})
}图片描述
写回答
1回答
-
我猜测问题出在 req.body 的解析上,你要确定 req.body 解析之后有 title 和 content 的内容。从这里入手查一下。
012019-06-23
相似问题