How to fix “TypeError: app.use() requires a middleware function”?

145 Views Asked by At

This is my server.js file and api.js file. I am getting an error in the sort function in which I intend to search the js objects according to their attributes.The event Schema has the attributes as name, location, price, rating. I tried to sort it according to their prices. “TypeError: app.use() requires a middleware function”?

index router

router.post('/add_products', function (req, res) {

  var imageFile = typeof req.files.image !== "undefined" ? req.files.image.name : "";

  req.checkBody('productName', 'product name must have a value.').notEmpty();
  req.checkBody('information', 'information must have a value.').notEmpty();
  req.checkBody('category', 'category must have a value.').notEmpty();
  req.checkBody('price', 'Price must have a value.').isDecimal();
  req.checkBody('image', 'You must upload an image').isImage(imageFile);

  var userId = req.user._id
  var productName = req.body.productName;
  var slug = title.replace(/\s+/g, '-').toLowerCase();
  var information = req.body.information;
  var price = req.body.price;
  var category = req.body.category;




  const errors = validationResult(req);
  if (!errors.isEmpty()) {

    var messageValidation = []
    for (var i = 0; i < errors.errors.length; i++) {
      messageValidation.push(errors.errors[i].msg)
    }
    console.log(messageValidation);
    req.flash('signupError', messageValidation)
    res.render('add_products',{
              errors:errors,
              userId : userId,
              productName: productName,
              information: information,
              category: category,
              price: price
    })
    return;
  }
 else {
      Product.findOne({slug: slug}, function (err, product) {
          if (product) {
              req.flash('danger', 'Product title exists, choose another.');
              Category.find(function (err, categories) {
                  res.render('add_products', {
                      title: title,
                      desc: desc,
                      categories: categories,
                      price: price
                  });
              });
          } else {

              var price2 = parseFloat(price).toFixed(2);

              var product = new Product({
                  title: title,
                  slug: slug,
                  desc: desc,
                  price: price2,
                  category: category,
                  image: imageFile
              });

              product.save(function (err) {
                  if (err)
                      return console.log(err);

                  mkdirp('public/product_images/' + product._id, function (err) {
                      return console.log(err);
                  });

                  mkdirp('public/product_images/' + product._id + '/gallery', function (err) {
                      return console.log(err);
                  });

                  mkdirp('public/product_images/' + product._id + '/gallery/thumbs', function (err) {
                      return console.log(err);
                  });

                  if (imageFile != "") {
                      var productImage = req.files.image;
                      var path = 'public/product_images/' + product._id + '/' + imageFile;

                      productImage.mv(path, function (err) {
                          return console.log(err);
                      });
                  }

                  req.flash('success', 'Product added!');
                  res.redirect('/admin/products');
              });
          }
      });
  }

});

in app js

// Express Validator middleware
app.use(expressValidator({
  errorFormatter: function (param, msg, value) {
      var namespace = param.split('.')
              , root = namespace.shift()
              , formParam = root;

      while (namespace.length) {
          formParam += '[' + namespace.shift() + ']';
      }
      return {
          param: formParam,
          msg: msg,
          value: value
      };
  },
  customValidators: {
      isImage: function (value, filename) {
          var extension = (path.extname(filename)).toLowerCase();
          switch (extension) {
              case '.jpg':
                  return '.jpg';
              case '.jpeg':
                  return '.jpeg';
              case '.png':
                  return '.png';
              case '':
                  return '.jpg';
              default:
                  return false;
          }
      }
  }
}));

what can i do to fix this issue

0

There are 0 best solutions below