Home > OS >  Getting Empty Object during Put request to the server
Getting Empty Object during Put request to the server

Time:01-31

I created an API for following and followers user for Social Media Application, while request from postman getting empty object: {} But it seems to me to be correct.

Model:

const mongoose = require("mongoose");

const UserSchema = mongoose.Schema({
  username: {
    type: String,
    required: true,
  },
  password: {
    type: String,
    required: true,
  },
  firstname: {
    type: String,
    required: true,
  },
  lastname: {
    type: String,
    required: true,
  },
  isAdmin: {
    type: Boolean,
    default: false,
  },
  profilePicture: String,
  coverPicture: String,
  about: String,
  livesin: String,
  workAt: String,
  relationship: String,
  followers: [],
  following: []
},{
    timestamps:true
}
);

const UserModel = mongoose.model("Users", UserSchema);

module.exports = UserModel;

UserControler:

const UserModel = require("../Models/Usermodel");
const bcrypt = require("bcryptjs");

const followUser = async (req, res) => {
  const id = req.params.id.trim();
  const { currentUserId } = req.body;

  if (currentUserId === id) {
    res.status(403).send("Action forbiden");
  } else {
    try {
      const followUser = await UserModel.findById(id);
      const followingUser = await UserModel.findById(currentUserId);

      if (!followUser.followers.includes(currentUserId)) {

        await followUser.updateOne({ $push: { followers: currentUserId } });
        await followingUser.updateOne({ $push: { following: id } });
        res.status(200).send({message:"User Followed"});
      } else {
        res.status(403).send("User alredy followed by you!");
      }
    } catch (error) {
      res.status(500).send(error);
    }
  }
};

module.exports = { getUser, updateUser, userDelete, followUser };

UserRoute:

const express = require("express");
const {getUser,updateUser, userDelete, followUser} = require("../Controller/userControler");

const router = express.Router()

router.get("/:id",getUser)
router.put("/:id",updateUser)
router.delete("/:id", userDelete)
router.put("/:id/follow", followUser)

module.exports=router;

index.js:

app.use("/user",UserRoute)

Here is the complete details regarding the error, let me know what happens in the code, thank you.

CodePudding user response:

please try findByIdAndUpdate query insted of using updateOne

CodePudding user response:

i assume that you have all the other functions other than followUser in your controller.js

The thing is that you must first specify the field name on the basis of which you want to update the document. Here is what you need to do;

const UserModel = require("../Models/Usermodel");
const bcrypt = require("bcryptjs");
const mongoose = require("mongoose");//updated line

const followUser = async (req, res) => {
  const id = req.params.id.trim();
  const { currentUserId } = req.body;

  if (currentUserId === id) {
    res.status(403).send("Action forbiden");
  } else {
    try {
      const followUser = await UserModel.findById({_id: mongoose.Types.ObjectId(id)});
      const followingUser = await UserModel.findById({_id: mongoose.Types.ObjectId(currentUserId)});

      if (!followUser.followers.includes(currentUserId)) {

        await followUser.updateOne({_id: mongoose.Types.ObjectId(*id of the user you want to update*)},{ $push: { followers: currentUserId } });
        await followingUser.updateOne({_id: mongoose.Types.ObjectId(*id of the user you want to update*)}{ $push: { following: id } });
        res.status(200).send({message:"User Followed"});
      } else {
        res.status(403).send("User alredy followed by you!");
      }
    } catch (error) {
      res.status(500).send(error);
    }
  }
};

module.exports = { getUser, updateUser, userDelete, followUser };

And while hitting the api pls make sure that your route should be

localhost:port-number/user/12345789/follow

and also make sure that the API type in postman must be same as in the backend e.g; PUT

  • Related