如何解决如何解决“ TypeError:app.use需要中间件功能”?
这是我的server.js文件和api.js文件。我在排序函数中出错,我打算根据其属性搜索js对象。事件模式具有名称,位置,价格,等级等属性。我试图根据他们的价格对其进行排序。 “ TypeError:app.use()需要中间件功能”吗?
索引路由器
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,price: price2,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');
});
}
});
}
});
在应用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;
}
}
}
}));
我该怎么做才能解决此问题
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。