skip to Main Content

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!')
}

2

Answers


  1. Chosen as BEST ANSWER

    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!")
    }
    

  2. const blacklistWord = await blacklistwordtextthingschema.findOne({ id: request.params.id });
    if (blacklistWord && request.body && request.body.someField.includes(blacklistWord.text)) {
        console.log('INCLUDES!');
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search