问题描述
router.post('/postComment/:id',postController.comment)
和postController.js注释函数,在其中创建注释对象并从Comment.js模型执行create方法:
> exports.comment = async function(req,res){
>
> var data = {
> author: req.body.author,> message: req.body.message,> postId: req.session.postId.id
> }
> console.log(data)
>
> let comment = new Comments(data)
>
> comment.create().then(() => {
> res.json(data)
> }).catch(() => {
> res.json([])
> }) }
和Comment.js模型:
const commentsCollection = require('../db').db().collection("Comments")
const ObjectID = require('mongodb').ObjectID
const sanitizeHTML = require('sanitize-html')
let Comments = function(data) {
this.data = data
this.errors = []
}
Comments.prototype.cleanUp = function(){
this.data = {
author: sanitizeHTML(this.data.author),message: sanitizeHTML(this.data.message),createdDate: new Date(),postId: this.data.postId
}
}
Comments.prototype.validate = function(){
if (this.data.author == "") {this.errors.push("you must provide a author")}
if (this.data.message == "") {this.errors.push("you must provide a message")}
}
Comments.prototype.create = function(){
return new Promise((resolve,reject) => {
this.cleanUp()
this.validate()
if(!this.errors.length){
commentsCollection.insertOne(this.data).then(() => {
resolve()
}).catch(() => {
this.errors.push("db problem")
reject(this.errors)
})
} else {
reject(this.errors)
}
})
}
Comments.viewCommentsByID = function(id){
return new Promise( async (resolve,reject) => {
let comment = await commentsCollection.find({postId: id}).sort({createdDate: -1}).toArray({},function(err,p){
})
resolve(comment)
})
}
module.exports = Comments
我想在我的网站上添加一个验证码,以防止僵尸程序发布。我调查了Google验证码,但无法正常工作。在我的网站上一切都很好,我只想继续使用验证码就可以继续运行。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)