Nodejs CRUD + Test using Postman

node js crud mysql
node js crud framework
node js crud mysql bootstrap
node js crud generator mysql
node js crud api mysql
node js crud example with sql server
node js tutorial
node.js mongodb example application

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


How to Build a Generalist CRUD API in NodeJS, The acronym CRUD (CREATE, READ, UPDATE, and DELETE) is an abbreviation for the most common operations to be performed on databases� In this tutorial, we are going to make a CRUD app using NodeJS and MongoDB. This will be a simple employee management web app with basic CRUD operations.

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

Tutorial: Build a Basic CRUD App with Node.js, Learn how to securely store, update, and display user data in a simple Node.js / Express.js app. Learn how to create CRUD application with nodejs + mongodb Tutorial & source code on - https://www.webtutorials.me/node-mongodb-crud-operation-tutorial/ Conn

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.

Create CRUD Application in Express JS - Vaibhav Sharma, JS ( Node JS Framework ). In this story, we can create a CRUD (Create, Read, Update, Delete) APIs. If you are new in the Node JS and… NodeJs and PostgreSQL CRUD example introduction Welcome to “NodeJs and PostgreSQL CRUD Example Application (Part 1),” a multi-part tutorial series. Every application must be able to perform CRUD at an elemental level. This tutorial will explain the process of setting up a NodeJs app and also give a NodeJs PostgreSQL CRUD example.

Node.js + MySQL CRUD - GET,POST,PUT and DELETE, MySQL CRUD Operations in Node.js application. In this tutorial, we'll discuss how to Duration: 28:25 Posted: Apr 23, 2018 Building a Simple CRUD Restful app with Node, Express, and MongoDB. CRUD stands for Create, Read, Update and Delete. These are the basic operations that a simple web app would be designed to achieve.

Building a Simple CRUD app with Node, Express, and MongoDB , Express is a framework for building web applications on top of Node.js. It simplifies the server creation process that is already available in Node. This is another nodejs and express.js tutorial which help to create CRUD operation using Monodb and Mongoose ORM. Mongodb is popular opensource no-SQL database.Mongoose is ORM (Object-relational mapping) that provide helpful methods to do operation with mongodb collections.

7 Steps to Create Simple CRUD application using Node.js, Express , Introduction. This is important! In this tutorial, you will learn how to create CRUD operations with node.js and mysql. If you haven� Hello world!. In this tutorial, we are going to build a simple CRUD application using Node JS and MySQL. What we will build? The image above shows the app. It is an application that lets you add players to a database and also display their details from the database. You can also delete and edit player details. Prerequisites

Comments
  • github.com/osern/ecom-react Here is the full project and there are some missing semicolons ; but i fixed them and it still does not work
  • did you consider using joi
  • console.log(fields) in your backend, fast and check which fileds are coming empty , you need to debug man
  • lol your shipping is false that;s why haha, you need to work on that shipping payload lol
  • i tried your suggestion and it still gives error: "All fields are required"
  • then one thing just before this if condition , can you console.log(fields) ?
  • i did that and it showed on the cmd { 'name': 'Node car', description: 'My first car on nodejs', price: '2000', category: 'category id number', quantity: '5', shipping: 'false' basically showed the fields i wrote on postman
  • can you try remove just ths "shipping" field from if () and re run the server.. just wanna be sure is something wrong with shipping as a boolean
  • if ( !label || !description || !price || !category || !quantity || shipping!== true || shipping !==false ) { return res.status(400).json({ error: "All fields are required" }); } Actually this was the problem now. When i remove it, the code works.....guess i have a lot of debugging to do :/
  • All i got was empty brackets and the same error. ill upload the image to show you
  • it means your backend is not receiving any data please check whether postman is firing the http request to the right url, especially the port on which the server is runining for example http:\\localhost:3000\read
  • I was able to create a category and it showed on the database, however when creating a product it just does not work. The server is running and db is connected and the url is correct. Just can't figure out why any of this is happening
  • This is not an answer. Please put such recommendations in the comments section.
  • What do you suggest i do for the problem that is?