使用Postman进行Nodejs CRUD +测试 [英] Nodejs CRUD + Test using Postman

查看:227
本文介绍了使用Postman进行Nodejs CRUD +测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

产品创建-测试我能够创建一个类别,但是在创建产品时,我使用邮递员对其进行了测试并显示错误:,然后添加了要求显示所有字段的代码。它们都是,除了消息不断弹出错误:所有字段都是必需的。我已经尝试了以下所有解决方案,但是没有一个对我有用吗?

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 all fields be present. They all were,except the message keeps popping up error: "All fields are required". I have tried all the below solutions and none of them worked for me any ideas?

const formidable = require("formidable");
const _ = require("lodash");
const fs = require("fs");
const Product = require("../models/product");
const { errorHandler } = require("../helpers/dbErrorHandler");

exports.productById = (req, res, next, id) => {
    Product.findById(id).exec((error, product) => {
        if (error || !product) {
            return res.status(400).json({
                error: "Product not found"
            });
        }
        req.product = product;
        next();
    });
};

exports.read = (req, res) => {
    req.product.photo = undefined;
    return res.json(req.product);
};

exports.create = (req, res) => {
    let form = new formidable.IncomingForm();
    form.keepExtensions = true;
    form.parse(req, (error, fields, files) => {
        if (error) {
            return res.status(400).json({
                error: "Image could not be uploaded"
            });
        }
        // check for all fields
        const {
            name,
            description,
            price,
            category,
            quantity,
            shipping
        } = fields;

        if (
            !name ||
            !description ||
            !price ||
            !category ||
            !quantity ||
            !shipping
        ) {
            return res.status(400).json({
                error: "All fields are required"
            });
        }

        let product = new Product(fields);

        // 1kb = 1000
        // 1mb = 1000000

        if (files.photo) {
            // console.log("FILES PHOTO: ", files.photo);
            if (files.photo.size > 1000000) {
                return res.status(400).json({
                    error: "Image should be less than 1mb in size"
                });
            }
            product.photo.data = fs.readFileSync(files.photo.path);
            product.photo.contentType = files.photo.type;
        }

        product.save((error, result) => {
            if (error) {
                return res.status(400).json({
                    error: errorHandler(error)
                });
            }
            res.json(result);
        });
    });
};

exports.remove = (req, res) => {
    let product = req.product;
    product.remove((error, deletedProduct) => {
      if (error) {
          return res.status(400).json({
              error: errorHandler(error)
          });
      }
      res.json({
          "message": "Product deleted successfully"
      });
    });
};


推荐答案

为什么不在每个要求上进行控制台登录,例如

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.

这篇关于使用Postman进行Nodejs CRUD +测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆