TypeError: Cannot read properties of undefined (reading 'newAge')

/////Server side

const express = require("express");
const app = express();
const cors = require("cors");
const mongoose = require("mongoose");
const FriendModel = require("./models/Friends");

app.use(cors());
app.use(express.json());

///////DATABASE CONNECTION
mongoose.connect("mongodb://127.0.0.1/merntutorialsDB", {
  useNewUrlParser: true
});

app.post("/addfriend", async (req, res) => {
  const name = req.body.name;
  const age = req.body.age;

  const friend = new FriendModel({
    name: name,
    age: age,
  });
  await friend.save();
  res.send("Successful");
});

app.get("/read", async (req, res) => {
  try {
    const result = await FriendModel.find({});
    res.send(result);
  } catch (err) {
    res.send(err);
  }
});

app.put("/update", async (req, res) => {
  const newAge = req.body.newAge;
  const id = req.body.id;

  // console.log(newAge, id)

  try {
      await FriendModel.findById( id).then((error, friendToUpdate) => {
      friendToUpdate.newAge;
      friendToUpdate.save();
      
    });
    
  } catch (err) {
    console.log(err);
  }

  res.send("updated");
});

app.listen(3001, () => {
  console.log("You are connected!");
});
```Preformatted text

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.