question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

formdata params , array when length is 1, typeof it is string

See original GitHub issue

Context

  • node version: 6.11.1
  • multer version: 1.3.0
  • environment (node, browser): windows

What are you trying to achieve or the steps to reproduce ?

when I post a array length is 1 , the server accept it as String, but when the legnth > 1, typeof it is Object

Server

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var multer = require('multer')

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(multer({ dest: './uploads/'}).any())

app.post('/', function(req, res, next){
  console.log("req.files.length", req.files.length)
  console.log("req.body",req.body)
  a = req.body.a; //the typeof a diffrent with it's length (but I want it as Object)
  console.log("a", typeof(a), a);
  return res.send(req.body || {})
});

// error handler
app.use(function(err, req, res, next) {
  res.status(err.status || 500).send("error" + err);
});

var server = app.listen(3000, function() {
  console.log("web server listening on port " + (server.address().port));
});

Test Demo

var assert = require("chai").assert
var request = require('request')

var url = "http://127.0.0.1:3000/"
describe("post", function(){
  it('array length 1', function(done){
    var opts = {
      method: 'POST',
      url: url,
      formData: {
        b: "hello",
        a: [1]
      }
    };
    request(opts, function(err, res, body){
      assert.equal(null, err);
      console.log("body", body);
      done()
    });
  })

  it('array length 2', function(done){
    var opts = {
      method: 'POST',
      url: url,
      formData: {
        b: "hello",
        a: [1 ,2 , 3]
      }
    };
    request(opts, function(err, res, body){
      assert.equal(null, err);
      console.log("body", body);
      done()
    });
  })
})

the first demo, typeof a is string, but the second a is Object,

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
peng-huang-chcommented, Sep 7, 2017

I am very grateful to you for your advice.

0reactions
LinusUcommented, Sep 6, 2017

body-parser doesn’t parse multipart/form-data, are you thinking of the application/x-www-form-urlencoded format? They are quite different, I would advice you to google them to find out how they look 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

appending array to FormData and send via AJAX
I'm using ajax to submit a multipart form with array, text fields and files. var attachments = document. getElementById('files'); var data= new FormData();...
Read more >
Using FormData Objects - Web APIs | MDN
The FormData object lets you compile a set of key/value pairs to send using XMLHttpRequest. It is primarily intended for use in sending...
Read more >
OpenAPI Specification - Version 2.0 - Swagger
The internal type of the array. The value MUST be one of "string" , "number" , "integer" , "boolean" , or "array" ....
Read more >
$_FILES - Manual - PHP
Last I checked (a while ago now admittedly), if you use array parameters in ... @param string $fileDescriptionParam (name, type, tmp_name, error или...
Read more >
Forms in HTML documents - W3C
Step one: Identify the successful controls; Step two: Build a form data set ... for text and passwd -- size CDATA #IMPLIED --...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found