0
app.post('/', (req, res) => {
    let myData = new contact(req.body)
});
myData.save().then(() => {
        res.send("This item has been saved to the database")
    })
    .catch(() => {
        res.status(400).send("item was not saved to the database")
    });

I am getting error myData is not defined


1 Answers1

0

You should modify the scope like this

app.post('/', (req, res) => {
 let myData = new contact(req.body)
  myData.save().then(() => {
    res.send("This item has been saved to the database")
  })
  .catch(() => {
    res.status(400).send("item was not saved to the databse")
  });
});

In your code, myData is out of the scope so you get the undefined error.

Pilpo
  • 1,236
  • 1
  • 7
  • 18