From 9f3e33fe11a54ecc6d1849fc7542cff47676ffbe Mon Sep 17 00:00:00 2001 From: Allen Lee Date: Wed, 19 Jul 2023 15:33:03 -0700 Subject: [PATCH] fix: switch to async event-driven I/O consuming the full POST data is sometimes incomplete otherwise --- app/pages/dataset/_id/index.vue | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/app/pages/dataset/_id/index.vue b/app/pages/dataset/_id/index.vue index 6baa844..df198f6 100644 --- a/app/pages/dataset/_id/index.vue +++ b/app/pages/dataset/_id/index.vue @@ -61,6 +61,22 @@ import Map from "@/components/dataset/Map.vue"; import { initializeDataset, saveGeoJson, clearGeoJson } from "@/store/actions"; +function getBody(request) { + const qs = require("querystring"); + return new Promise((resolve) => { + const bodyArray = []; + let body; + request + .on("data", (chunk) => { + bodyArray.push(chunk); + }) + .on("end", () => { + body = Buffer.concat(bodyArray).toString(); + resolve(qs.parse(body)); + }); + }); +} + @Component({ layout: "DefaultLayout", components: { @@ -150,20 +166,11 @@ class SelectDatasetArea extends Vue { } } - asyncData({ req, res }) { + async asyncData({ req, res }) { if (process.server) { if (req.method === "POST") { - const qs = require("querystring"); - let body = ""; - let data = ""; - while ((data = req.read())) { - body += data; - } - const postData = qs.parse(body); - console.log( - "XXX: posted geojson, now do something with it: ", - postData - ); + const postData = await getBody(req); + console.log("Received POST data: ", postData); if (postData) { try { const studyAreaGeoJson = JSON.parse(postData.studyArea); @@ -172,6 +179,8 @@ class SelectDatasetArea extends Vue { console.log("error parsing geojson: ", e); } } + let body = ""; + let chunk = ""; } else { console.log("not a post request"); }