Search code examples
javascriptnode.jsmongodbmongoose

How do i check if a string includes a blacklisted word that is in a mongodb collection


I want to check if a string includes a blacklisted text that is in a mongodb collection.

I have tried the code below and it is not logging it to console even if it does include the blacklisted text.

const includesit = await blacklistwordtextthingschema.find({id: `${request.params.id}`});
if(JSON.stringify(request.body).includes(includesit.text)){
    console.log('INCLUDES!')
}

Solution

  • I managed to get it working by making the blacklisted text into an array and pushing in the array instead of making a new document for every text.

    const blacklistWord = await blacklistwordtextthingschema.findOne({ id: request.params.id });
    if (!blacklistWord.text.some(v => JSON.stringify(request.body).includes(v))) {
     console.log("body does not include a blacklisted word!")
    }