我是Nodejs和RethinkDB的新手。我只是建立简单的功能。如果用户点击Like按钮它将增加喜欢如果他们已经喜欢,喜欢将删除。和安全我的API机智jwt令牌,我的问题是什么时候用户一次又一次点击按钮。就像只是增加我不知道为什么会发生这种情况
app.post('/__h',(req,res) => {
function deleteLike(username,name,postDbId) {
r.connect({db:'image'}).then(conn => {
r.table('posts').get(postDbId).update(
{
whoLikedIt:r.row('whoLikedIt').filter(liked => {
return liked('username').ne(username)
})
}
).run(conn).then(response => {
if(response.replaced == 1){
r.table('posts').get(postDbId).update({
likesCount:r.row("likesCount").add(-1)
}).run(conn).then(response => {
console.log(response)
res.json({
isLiked:false
})
})
console.log(response)
}else{
res.json({
isLiked:false
})
}
})
})
}
function postLike(username,name,postDbId) {
r.connect({db:'image'}).then(conn => {
r.table('posts').get(postDbId).update(
{whoLikedIt:r.row('whoLikedIt').append({name:name,username:username})}
).run(conn).then(response => {
if(response.replaced == 1){
r.table('posts').get(postDbId).update({
likesCount:r.row("likesCount").add(1)
}).run(conn).then(response => {
console.log(response)
res.json({
isLiked:true
})
})
}else{
res.json({
isLiked:false
})
}
})
})
}
let isLoggedIn = req.cookies['check'];
let token = req.cookies['peace'];
if(isLoggedIn == "true"){
if(token){
jwt.verify(token,JWTPASS,(err,decode) => {
if(err){
res.json({error:true})
}else{
//Check is that Liked
let username = decode.user.username;
let name = decode.user.name;
let postDbId = req.body.data.postDbId;
r.connect({db:'image'}).then(conn => {
r.table('posts').get(postDbId).run(conn).then(response => {
// console.log(response);
if(response.whoLikedIt.length == 0){
console.log('No One Likes')
postLike(username,name,postDbId);
}else{
response.whoLikedIt.map(data => {
if(data.username == username){
console.log('Already Liked');
deleteLike(username,name,postDbId)
}
})
}
})
})
}
})
}else{
console.log('Dei')
res.json({error:true});
}
}else{
res.json({error:true});
console.log('OMG')
}
})