【问题标题】:Nodejs CRUD + Test using PostmanNodejs CRUD + 使用 Postman 进行测试
【发布时间】:2020-02-28 06:50:26
【问题描述】:

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

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"
      });
    });
};

【问题讨论】:

  • github.com/osern/ecom-react 这是完整的项目,缺少一些分号;但我修复了它们,它仍然无法正常工作
  • 你考虑过使用joi
  • console.log(fields) 在你的后端,快速检查哪些文件是空的,你需要调试 man
  • 大声笑,你的运费是 false;这就是为什么哈哈,你需要处理运输有效载荷 lol

标签: node.js postman


【解决方案1】:

为什么不控制台登录每个请求,例如

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);
};

您将清楚地了解您的请求是否真正被您的后端接收。

【讨论】:

  • 我得到的只是空括号和同样的错误。生病上传图片给你看
  • 这意味着您的后端没有收到任何数据请检查邮递员是否将http请求发送到正确的url,尤其是服务器正在运行的端口,例如http:\\localhost:3000\阅读
  • 我能够创建一个类别并显示在数据库中,但是在创建产品时它只是不起作用。服务器正在运行,数据库已连接,并且 url 正确。只是无法弄清楚为什么会发生这种情况
  • 这不是答案。请将此类建议放在 cmets 部分。
  • 你建议我为这个问题做些什么?
【解决方案2】:
 if (
            !name ||
            !description ||
            !price ||
            !category ||
            !quantity ||
            !shipping
        ) {
            return res.status(400).json({
                error: "All fields are required"
            });
        }

你从邮递员那里发送了虚假的运费,显然它会返回 400 状态,让它:

 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"
            });
        }
    ```


【讨论】:

  • 我试过你的建议,但它仍然给出错误:“所有字段都是必需的”
  • 那么在此之前的一件事 if 条件,你能 console.log(fields) 吗?
  • 我这样做了,它显示在 cmd { 'name': 'Node car', description: 'My first car on nodejs', price: '2000', category: 'category id number' , 数量: '5', shipping: 'false' 基本显示了我在邮递员上写的字段
  • 你可以尝试从 if () 中删除“运输”字段并重新运行服务器.. 只是想确定作为布尔值运输有问题
  • if ( !label || !description || !price || !category || !quantity || shipping!== true || shipping !==false ) { return res.status(400 ).json({ 错误:“所有字段都是必需的” });实际上这是现在的问题。当我删除它时,代码可以工作......我想我有很多调试要做:/
【解决方案3】:

您的问题在于在邮递员中设置内容类型。您将表单数据发送到服务器,但您的服务器未对其进行解码。默认情况下,您的服务器会获得 x-www-form-urlencoded,如果您在 x-www-form-urlencoded 上设置邮递员,您的问题就消失了。 您必须使用表单数据上传文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-23
    • 2018-05-18
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多