admin管理员组

文章数量:1290978

I am having an issue with a route in my backend where res.status().send() will only send the client the status code, but it will not send the client the object located inside of send().

Here is my code (redacted all code but the problem for brevity):

exports.user_signup = (req, res) => {
  const { body } = req;
  const { panyName, password, email } = body;

  User.find({ email: email }, (err, previousUsers) => {
    if (err) {
      return res.status(400).send({
        message: "There was an issue signing up."
      });
    } else if (previousUsers.length > 0) {
      return res.status(403).send({
        message: "Records show this email is linked to another account."
      });
    }
}

When I make my fetch request from the client, the response only returns the status code from the server, but nowhere in the response is the object in the send() method on the server. Just spitballing, I threw res.status(200).json(object) at it to send the object as json to no avail.

Here is my `fetch request from the client:

  fetch("http://localhost:3000/users/accounts/", {
    method: "post",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    body: JSON.stringify(userData)
  }).then(response => console.log(response));
}

To show what response I am getting, I purposely posted some form data from the client to the route that would throw the 403 error, and this is the response I get in the browser console:

Response {type: "basic", url: "http://localhost:3000/users/accounts/", redirected: false, status: 403, ok: false, …}

So I am able to successfully send the status back from the route to the client, however I can not for the life of me figure out why send() does not send the object along with it.

I am having an issue with a route in my backend where res.status().send() will only send the client the status code, but it will not send the client the object located inside of send().

Here is my code (redacted all code but the problem for brevity):

exports.user_signup = (req, res) => {
  const { body } = req;
  const { panyName, password, email } = body;

  User.find({ email: email }, (err, previousUsers) => {
    if (err) {
      return res.status(400).send({
        message: "There was an issue signing up."
      });
    } else if (previousUsers.length > 0) {
      return res.status(403).send({
        message: "Records show this email is linked to another account."
      });
    }
}

When I make my fetch request from the client, the response only returns the status code from the server, but nowhere in the response is the object in the send() method on the server. Just spitballing, I threw res.status(200).json(object) at it to send the object as json to no avail.

Here is my `fetch request from the client:

  fetch("http://localhost:3000/users/accounts/", {
    method: "post",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    body: JSON.stringify(userData)
  }).then(response => console.log(response));
}

To show what response I am getting, I purposely posted some form data from the client to the route that would throw the 403 error, and this is the response I get in the browser console:

Response {type: "basic", url: "http://localhost:3000/users/accounts/", redirected: false, status: 403, ok: false, …}

So I am able to successfully send the status back from the route to the client, however I can not for the life of me figure out why send() does not send the object along with it.

Share Improve this question asked Nov 30, 2018 at 5:40 maison.mmaison.m 8634 gold badges21 silver badges40 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

The body of the response that es back from fetch() is a ReadableStream. You need to process it to turn it into something usable. Normally you would call response.json() to parse it as a JSON object:

fetch("http://localhost:3000/users/accounts/", {
    method: "post",
    headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
    },
    body: JSON.stringify(userData)
})
    .then(response => response.json())
    .then(response => console.log(response));
}

本文标签: javascriptNodeExpressresstatus()send() only sends the status but does not send an objectStack Overflow