-1

I tried this code in node js:

const express = require('express');

const bodyParser = require('body-parser');

const app = express();

app.use(express.urlencoded({extended: false}))
app.use(express.json())


 app.post("/Picture", (req, res, next) => {


        if(req.body == { aaa: 'aaa'}){

            res.send(body)
            console.log("well done!!!!")
           

        }


      
    })


I want when user enter in body this json object { aaa: 'aaa'} then get response but this code not work I dont know how i can make user can only send specific json objects

esqew
  • 42,425
  • 27
  • 92
  • 132
Mohamad Eibo
  • 5
  • 1
  • 6
  • For such a scenario, it's better to declare some DTOs. Check this: [https://github.com/typestack/class-validator] (class-validator) – Emad Mamaghani Jan 26 '22 at 14:40
  • 1
    It would be helpful if you would tell us what the `req.body` variable contains when receiving a request. – SoulKa Jan 26 '22 at 14:52

1 Answers1

0

Do you want to check that the body contains a property "aaa" with the string "aaa"?

if (typeof req.body === "object" && req.body != null && req.body.aaa === "aaa") {
    res.send(body)
    console.log("well done!!!!")
}

Comparing an object to another object with obj == { ... } does not check if the two objects are of the same structure but instead check if they are the same object instance!

Edit: Maybe this thread helps

SoulKa
  • 187
  • 6