Nodejs CRUD + Test using Postman

后端 未结 3 819

Product create - Test I was able to create a category, however when creating a product i tested it out using postman and got error: \"\", then i added the code which requires al

相关标签:
3条回答
  • 2021-01-27 15:16

    why not console log on every req i.e for example

    exports.productById = (req, res, next, id) => {
        console.log(req.body);
    };
    
    exports.read = (req, res) => {
        console.log(req.body);
    };
    
    exports.create = (req, res) => {
        console.log(req.body);
    };
    
    exports.remove = (req, res) => {
        console.log(req.body);
    };
    

    you will get a clear picture whether your request are actually received by your backend.

    0 讨论(0)
  • 2021-01-27 15:20

    your problem is on setting content type in postman. you send form-data to the server but your server not decoded it. by default, your server gets x-www-form-urlencoded and if you set postman on x-www-form-urlencoded your problem is gone. you must use form-data for upload file

    0 讨论(0)
  • 2021-01-27 15:28
     if (
                !name ||
                !description ||
                !price ||
                !category ||
                !quantity ||
                !shipping
            ) {
                return res.status(400).json({
                    error: "All fields are required"
                });
            }
    

    you are sending shipping false from postman , obviously it will return that 400 status, make it :

     if (
                !name ||
                !description ||
                !price ||
                !category ||
                !quantity ||
               shipping!== true || shipping !==false //something like that(or use something from formidable)
            ) {
                return res.status(400).json({
                    error: "All fields are required"
                });
            }
        ```
    
    
    
    0 讨论(0)
提交回复
热议问题