0

Once I submit the value "Tokyo" selected in the form, the value which receives at node.js includes unnecessary character "\u001c" before "Tokyo".

Right Value : "Tokyo"
Wrong Value : "\u001cTokyo"

I would like to save the right value in Monogodb.

As a tentative solution, I tried to delete the only unnecessary characters and save the value in mongodb, but it didn't work. req.body.prefecture.replace(/\u001c/g, "");

To make sure what is happening, I have dumped the value which received at node.js from html.

Front End : Html

<form action="/places/update" method="post" enctype="multipart/form-data">

 <select class="form-control" id="prefecture" name="prefecture">
  <option value="Tokyo" selected>Tokyo</option>
  <option value="Chiba">Chiba</option>
  <option value="Kanagawa">Kanagawa</option>
 </select>

 <button type="submit" class="btn btn-success">Register</button>
</form>

Back End : node.js

router.post("/update", upload.single("file"), (req, res) => {
 console.log(req.body);
 console.log(req.body.prefecture);
 console.log("Tokyo");
});

Log

[Object: null prototype] {
  prefecture: '\u001cTokyo'
}
Tokyo
Tokyo

Could you let me know why this happen and how to get correct value?

Tsubasa Yamaguchi
  • 177
  • 1
  • 2
  • 10

1 Answers1

2

What I understand is that a multipart http Post request is sent with a body that is specially formatted as a series of "parts" that are separated by boundary strings (see https://ec.haxx.se/http-multipart.html).

The \u001cTokyo part is an information separator (see https://codepoints.net/U+001C).

So, I think, it serves as a separator of those "parts" that is added in the Content type header.

So maybe you can make the Post request without the enctype? As proposed by others.

Karlan
  • 353
  • 1
  • 2
  • 10
  • Thank you. I put a comment above, but I am using multer upload.single("file") as middleware. I should not upload a file and send a data together? – Tsubasa Yamaguchi Sep 01 '19 at 19:47
  • I don't think that is the problem but using multipart you always get boundary separators. I found this helpful: https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean – Karlan Sep 01 '19 at 20:23
  • But can't you just save req.body.prefecture to de MongoDB database. I have never used Multer but I see in your console.log(req.body.prefecture) that the output is what you want. I don't think you want to save the body Object. – Karlan Sep 01 '19 at 20:48
  • I put the the value in temp and then put it in monodb object, but it failed. `temp = req.body.prefecture` `mangodb.prefecture = temp` `console.log(mangodb) => {prefecture: \u001cTokyo }` – Tsubasa Yamaguchi Sep 03 '19 at 13:41
  • ok if found this ```temp.replace(/[^\x20-\x7E]/g, '') ``` on https://www.w3resource.com/javascript-exercises/javascript-string-exercise-32.php – Karlan Sep 03 '19 at 14:17
  • Thank you. This works for me. `restaurant = { prefecture: req.body.prefecture.replace("\u001c", "") } ` – Tsubasa Yamaguchi Sep 03 '19 at 14:43