Reputation: 366
What I'm trying to find is first select those comments which contain my search param it will return postId's then select the whole post with all comments including that comment also which we've searched using search param!
app.get('/search_', (req, res, next) => {
var search = req.query.search;
comment.findAll({
attributes: ['PostId'],
where: {
the_comments: {
[Op.like]: `%${search}%`
}
}
})
.then(data => {
var array = [ ];
for (var x in data) {
console.log(data[x].PostId);
array.push(data[x].PostId);
}
console.log(array);
Post.findAll({
include: [
{
model: comment
}
],
where: {
id: {
[Op. in]: [array]
}
}
})
.then(result => {
res.json(result);
})
})
});
Upvotes: 0
Views: 2510
Reputation: 1004
Post.findAll({
include: [{
model: comment
}],
where : {
id : {
[Op.in] : [connection.literal(`select PostId from comment where the_comments like '%${search}%'`)]
}
}
})
.then(result => {
console.log('result',result);
res.json(result);
}).catch(error=>{
console.log(error);
res.json(error);
})
Upvotes: 1
Reputation: 29
You can try this.
Post.findAll({
include: [
{
model: comment,
required: true,
where: {
the_comments: {
[Op.like]: `%${search}%`
}
}
}
]
})
Upvotes: 1