无法在nodejs和firebase函数中使用busboy处理POST方法

iyr7buue  于 2023-01-12  发布在  Node.js
关注(0)|答案(1)|浏览(109)

嗨,我尝试使用post方法与busboy的多部分表单数据,即使代码是完美的工作在测试它在localhost上,但当运行在firebase函数,我得到下面的错误:
在 Postman 我得到的React是:错误:无法处理请求
如果你还需要我提供更多信息请告诉我。

Error: Unexpected end of form
    at Multipart._final (/workspace/node_modules/busboy/lib/types/multipart.js:588:17)
    at callFinal (node:internal/streams/writable:696:27)
    at prefinish (node:internal/streams/writable:725:7)
    at finishMaybe (node:internal/streams/writable:735:5)
    at Multipart.Writable.end (node:internal/streams/writable:633:5)
    at onend (node:internal/streams/readable:693:10)
    at processTicksAndRejections (node:internal/process/task_queues:78:11)

如果有人能帮我解决这个问题
下面是代码:
index.js

var busboy = require('connect-busboy');

app.use(busboy());

app.use(cors()); 

const portCheck = process.env.PORT || 3001

app.use(express.json({limit: "50mb"}));
app.use(express.urlencoded({limit: "50mb", extended: true}));

app.use(cookieParser());
// app.use(express.static(path.join(__dirname, '../public-flutter')));
// app.get('/', (_, res) => {
//   res.sendFile(path.resolve(__dirname, '../public-flutter/index.html'));
// });

userProfile.js

router.post('/userprofile/check/busboy', async (req,res) => {
  console.log(req.body);
  
  const fields = {};
  req.busboy.on('field', (name, val) => {
    console.log('reached till here');
    
      console.log(`Processed field ${name}: ${val}.`);
    
    
    //fields.set(name,val)
    fields[name] = val;
    console.log(fields);
  });

  req.busboy.on('finish', function() {
    console.log('Done parsing form!');
    res.status(201).send();
  });
  
  req.pipe(req.busboy);
})
bzzcjhmw

bzzcjhmw1#

req.busboy.on('field', (name, val) => {
      
        fields[name] = val;
      
    });

    req.busboy.on('finish', () => {
              
                  try {
                    
                    notify = new userCampaign(fields);
                    notify.save((err,post) => {
                      if(err) {
                       console.log(err);
                      } 
                     
                      datacombine = new userCampaign(
                        {
                          title: post.title,
                          status: post.status,
                        }
                        )
                        datacombine.save((err,postcombine) => {
                          if(err) {
                            console.log(err);
                          }
                        res.status(201).send(postcombine);
                      })  

                     });
                  }
                  catch(e){
  
                  }
            })
            
            req.busboy.end(req.rawBody);
            req.pipe(req.busboy)
})

相关问题