正文解析器未定义,要求正文

时间:2018-11-02 15:25:55

标签: javascript node.js express body-parser

所以我有这个发布路线,可以将一些产品添加到我的MongoDB

library(cowplot)

theme_set(theme_bw())

p.bottom <- ggplot(dat, aes(x = x, y = y)) +
  geom_line(size = 1) +
  scale_y_log10(breaks = c(0.001, 0.01, 0.1, 0.5),
                expand = c(0, 0),
                sec.axis = sec_axis(trans = ~ . * (1/2),
                                    breaks = c(0.001, 0.01, 0.1, 0.25))) +
  coord_cartesian(ylim = c(0.001, 0.5)) + # limit y-axis range
  theme(axis.text.y=element_text(colour = "black", size=15),
        axis.title.y = element_blank(),
        axis.ticks.length = unit(0, "pt"),
        plot.margin = unit(c(0, 5.5, 5.5, 5.5), "pt")) #remove any space above plot panel

p.top <- ggplot(dat, aes(x = x, y = y)) +
  geom_line(size = 1) +
  scale_y_continuous(breaks = c(0.6, 0.7, 0.8, 0.9, 1),
                     labels = function(y) sprintf("%.3f", y), #ensure same label format as p.bottom
                expand = c(0, 0),
                sec.axis = sec_axis(trans = ~ . * (1/2),
                                    breaks = c(0.3, 0.35, 0.4, 0.45, 0.5),
                                    labels = function(y) sprintf("%.3f", y))) +
  coord_cartesian(ylim = c(0.5, 1)) + # limit y-axis range
  theme(axis.text.y=element_text(colour = "black", size=15),
        axis.text.x = element_blank(),       # remove x-axis labels / ticks / title &
        axis.ticks.x = element_blank(),      # any space below the plot panel
        axis.title.x = element_blank(),
        axis.ticks.length = unit(0, "pt"),
        plot.margin = unit(c(5.5, 5.5, 0, 5.5), "pt"))

plot_grid(p.top, p.bottom, 
          align = "v", ncol = 1)

我有这个(pug / jade)模板:

router.post('/addprod',mid.reqAdmin,function(req,res,next){
            if(req.body.category && req.body.name && req.body.price && req.body.description && req.files)
            {
                upload(req,res,function(err) {
                    console.log("req.body"); //form fields
                    console.log(req.body);
                    console.log("req.file");
                    console.log(req.files); //form files
                })
                var prodData={
                    name: req.body.name,
                    price: req.body.price,
                    description: req.body.description,
                    category: req.body.category,    
                    images: req.files.filename
                };
                console.log(prodData);
                Prod.create(prodData,function (error, product) {
                    if (error) {
                        req.flash('error','Error');
                        return res.redirect('/addprod');
                    } 
                    else{
                        req.flash('success', 'Success');
                        return res.redirect('/addprod');
                    }
                });         
            }else
            {
                req.flash('error','All Fields Required');
                return res.redirect('/addprod');
            }
        });

因此,当我单击“添加”按钮并填写所有字段时,我会收到“所有必填字段” 我在IF语句之前放了一些控制台日志,然后得到req.body.name = undefined 和req.body = {}。而且我不知道为什么我得到那个。 在app.js文件中,我有正文解析器。我的意思是在每条路线上都行,但此正文解析器有效。

1 个答案:

答案 0 :(得分:3)

body-parser不处理multipart/form-data。为此,您需要multer / formidable或类似的npm软件包。

express-formidable的示例:

快速配置中的某个地方

// ...

const formidableMiddleware = require('express-formidable');

//...

app.use(formidableMiddleware({
  encoding: 'utf-8',
  multiples: true
});

// ...

然后在控制器文件中,您可以使用req.fields访问文本字段(例如categoryname)和req.files访问二进制数据(例如{{1 }})

示例

images