How to make a map on an array of arrays and then filter, I have a mongoose model in which there is an array of objectId and I need to make a filter but being an array array I first made a map and then a filter and not it works for me
const mongoose = require('mongoose');
const constants = require('../constants');
const friendsSchema = mongoose.Schema({
users: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}],
status: {
type: String,
enum: [constants.status.PENDING, constants.status.FRIENDS, constants.status.BLOCKED],
default: constants.status.PENDING
},
owner: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
}, {timestamps: true});
const Friendship = mongoose.model('Friendship', friendsSchema);
module.exports = Friendship;
module.exports.list = (req, res, next) => {
const me = req.user._id;
Friendship.find({ users: { $in: [me] }, status: "PENDING" })
.populate('users')
.populate('owner')
.then(pendingRequests => {
if (pendingRequests && pendingRequests.length > 0) {
function getUserIsNotMe(){
return pendingRequests.map(element =>{
return element.users.filter(ele=>{
return !ele.equals(me);
});
});
}
let notMe = getUserIsNotMe();
console.log(notMe);
res.render("users/friendRequest", {notMe});
} else {
res.render("users/friendRequest", {
error: "You dont have friend requests"
});
}
})
.catch(error => {
if (error instanceof mongoose.Error.CastError) {
next(createError(404, 'friendship not found'));
} else {
next(error);
}
});
};