0 votes
1 view
in Web Technology by (14.5k points)

I'm trying to update a user's shipping information. I have default data in mongodb that I'm trying to overwrite with a PATCH/PUT. Right now postman hangs indefinitely. The mongoose model has the values, my else{} statement outputs the JSON object to be submitted with no error, but it never goes through. I appreciate any help. POSTMAN

Could not get any response

app.js

app.put('/api/users/:id', (req, res) => {

  User.findByIdAndUpdate({ _id: req.params.id },

    {

      fullName: req.body.fullName,

      address1: req.body.address1,

      address2: req.body.address2,

      city: req.body.city,

      state: req.body.state,

      zip: req.body.zip

    }, function (err, docs) {

      if (err) res.json(err);

      else {

       // console.log(docs)

      }

    });

})

MODEL

 const mongoose = require("mongoose");

const shippingSchema = mongoose.Schema({

  fullName: { type: String},

  address1: { type: String },

  address2: { type: String },

  city: { type: String },

  state: { type: String},

  zip: { type: String}

});

module.exports = mongoose.model("Shipping", shippingSchema);

1 Answer

0 votes
by (25.1k points)

You're forgetting to send a response, so the request never finishes.

app.put('/api/users/:id', (req, res) => {

  User.findByIdAndUpdate({ _id: req.params.id },

    {

      fullName: req.body.fullName,

      address1: req.body.address1,

      address2: req.body.address2,

      city: req.body.city,

      state: req.body.state,

      zip: req.body.zip

    }, function (err, docs) {

      if (err) res.json(err);

      else {

        // console.log(docs)

        // don't forget to send a response

        res.json({

          "message": "works!",

        });

      }

    });

})

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...