简体   繁体   中英

nodeJS code not functioning properly in API mode

I have CRUD code that is working perfectly on the server but when I turn these codes to the equivalent version of API, one of them, the postsCreate method used in submitting the code refuses to submit the data sent to the database as it keeps returning errors, so I console.log req.body.title, req.body.user and req.body.postContent to see what is happening; alas, to my surprise, it returns this { title: undefined, user: undefined, postContent: undefined } , so I console.log(req.body) and it returns this in the console:

[Object: null prototype] {
  '{\n"title": last last",\n"user": Gbenga",\n"postContent": "He go work"\n}': ''
}

This is the method. (It is the same as the server version.)

const postsCreate = (req, res) => {
  const tru = ({
    title: req.body.title,
    user: req.body.user,
    postContent: req.body.postContent
  });

  console.log(req.body);  // produces this [Object: null prototype] {'{\n"title": last last",\n"user": Gbenga",\n"postContent": "He go work"\n}': ''}
  console.log(tru);  // produces this { title: undefined, user: undefined, postContent: undefined }

  Post.create({
      title: req.body.title,
      user: req.body.user,
      postContent: req.body.postContent
    },
    (err, post) => {
      if (err) {
        res
          .status(400)
          .json(err);
      } else {
        res
          .status(201)
          .json(post);
      }
    });
};

What is really going on? Why is it submitting from the server directly and refuses to do so when connected from the API and finally, what can I do to solve it? PS I tested it through the POSTMAN app

add this line in your code in starting after imports

If you are using Express 4.16+
app.use(express.json()); //Used to parse JSON bodies

else install body-parser, import in the file, and use it

npm install --save body-parser

and

const bodyparser = require('body-parser');

app.use(bodyparser.json());

I think what's happening is that the body of the request isn't being parsed correctly. Notice that the value of request.body you have is an object that looks like this:

{
  "{ ... the data you want ... }": ""
}

The whole of the body appears as the name of a single property in the object.

So I think a body parser that expects the name=value pairs that come from a form is instead receiving JSON. While it's looking for a = it reads everything as a name, then because there's no = it uses an empty string as the value, making the object you see. It doesn't understand JSON, so it doesn't create properties for title, user and postContent, so those are undefined.

To fix this, make sure you have a body parser that understands JSON, as @r7r explains, and that the client sends its JSON with the header Content-Type: application/json .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM