我正在嘗試為博客文章構建一個 upvote/downvote 功能,無論用戶之前是否需要單擊按鈕。但是 javascript find()函式不起作用。有沒有辦法從陣列中獲取匹配的嵌入檔案?
索引.js
io.on( 'connection', function( socket ) {
console.log( 'a user has connected!' );
socket.on( 'disconnect', function() {
console.log( 'user disconnected' );
});
socket.on( 'upvote-event', async function( upvote_flag , id) {
console.log(typeof id); //getting the post id from button click
console.log(user.upvotes_downvotes); //upvote downvote array
const post_Id = new ObjectID(id);
const PreClicked = user.upvotes_downvotes.find(clicked => {
console.log(clicked.postId,post_Id)
return clicked.postId === post_Id; // where it is failing
});
var ok = false;
console.log(PreClicked, post_Id); //PreClicked is always undefined :(
if(PreClicked === undefined)
{
upvote_count = 1;
const newUpvote = { postId: post_Id, types: "upvote" };
// console.log(user._id);
await User.findOneAndUpdate({_id: user._id},
{ $push:
{
upvotes_downvotes: newUpvote
},
},
)
}else if(PreClicked.types==="upvote"){ //Never goes here even if the upvoted posts exists in array
upvote_count = -1;
await User.findOneAndUpdate({_id: user._id},
{ $pull:
{ upvotes_downvotes:{
$elemMatch:{
postId: post_Id, types: "upvote"
}
}
}
})
}
});
架構.js
const Mongoose = require('mongoose');
const Schema = Mongoose.Schema;
const passportLocalMongoose = require('passport-local-mongoose');
User = new Schema({
email : {
type : String,
required : true,
unique : true
},
upvotes_downvotes:[{
postId:{ type: Schema.Types.ObjectId , ref:'Post'},
types: String
}],
});
module.exports = Mongoose.model('User',User);
uj5u.com熱心網友回復:
發現解決方案“===”不適用于檔案并且適用于.equals
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/387193.html
標籤:javascript 节点.js MongoDB 插座 猫鼬
上一篇:ReactNativesocket.io無法偵聽“socket.on”偵聽器
下一篇:發送廣播訊息時的奇怪行為
