In my app I have a schema for comments:
var CommentsSchema = new Schema({
user_id: {type: String, required: true, ref: 'users'},
text_content: {type: String},
is_anonymous: {type: Boolean, default: false}
});
and now I'm constructing a mongoose query to download all comments and display it to the users.
Since I don't want to download posts from users that are blocked by the end user, I introduced a possibility of excluding posts from blocked authors:
var blockedUsers = req.body.blockedUsers;
function withBlockedUsers(query, blockedUsers) {
if(blockedUsers != undefined){
query.$and.push({ 'user_id' : { $nin: blockedUsers } });
}
return query;
}
var query = {};
query.$and = [];
query = withBlockedUsers(query, blockedUsers)
...
query = Comment.find(query);
query.exec(function(err, comments){
if(err) {
callback(err);
return;
}
return callback(null, comments);
});
That code works, when I call my endpoint I need to send there a string array of blocked user ids and their posts will be excluded.
Now I'm changing my functionality and instead of passing string array of blocked users I'm passing array of objects:
{
user_id: '586af425378c19fc044aa85f'
is_anonymous: '0'
},
I don't want to download posts from those users when those two conditions are met.
So for example when I have two posts in my app:
{
user_id: '586af425378c19fc044aa85f',
text_content: 'text1',
is_anonymous: true
},
{
user_id: '586af425378c19fc044aa85f', //same as above
text_content: 'text2',
is_anonymous: false
}
and I pass blockedUsers object:
{
user_id: '586af425378c19fc044aa85f'
is_anonymous: '0'
},
as a return I need to display only:
{
user_id: '586af425378c19fc044aa85f',
text_content: 'text1',
is_anonymous: true
},
The other post should be blocked because user_id is recognized and is_anonymous is false.
With my current code I'm getting error:
message: 'Cast to string failed for value "[object Object]" at path "user_id"', name: 'CastError', kind: 'string', value: { user_id: '586af425378c19fc044aa85f', is_anonymous: '0' }, path: 'user_id', reason: undefined } Cast to string failed for value "[object Object]" at path "user_id"