0

I want to send a MySql "DateTime" var to a server. The var should be in this format: YYYY-MM-DD HH:MM:SS. The problem is when I try to send the var with fetch API to express, I get something like this: YYYY-MM-DD&20HH:MM:SS.

I have not tried anything so far

client-side:

var fetch = require("node-fetch");

function connectToServer(params) {

    let url = "http://localhost:3000/" + params;

    return new Promise((resolve, reject) => {
        fetch(url).then(function(response) {
            response = response.json();
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
};


let user = "A";
let time = "2020-07-07 11:00:00";

connectToServer(`joinQueue/${user}/${time}`).then(result => {
console.log(result)

}).catch(error => {
console.log(error);

});

**server-side*

joinQueue(user, time) {
        const joinQueueQuery = `INSERT INTO queue VALUES("${user}", '${time}')`;

        return new Promise((resolve, reject) => {
            this.DB.queryDataBase(joinQueueQuery).then(result => {
                resolve(true);
            }).catch(error => {
                reject(error);
            });
        });
    }
}

when I print "joinQueueQuery", I get "2020-07-07T08:00:00.000Z" in the database. The problem is the hour in the database is 8 o'clock, although I sent 11.

Omar_Hafez
  • 272
  • 2
  • 13
Dan
  • 31
  • 1
  • 5
  • 3
    It's `%20`, not `&20`. That's called URI-encoding, and you need to decode it on the server. – Patrick Roberts Aug 04 '19 at 00:27
  • Also please read about and _avoid_ the [explicit promise construction antipattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it). – Patrick Roberts Aug 04 '19 at 00:29
  • 1
    `2020-07-07T08:00:00.000Z` is UTC time - you are in UTC+3 timezone - therefore `2020-07-07T08:00:00.000Z` **IS** `2020-07-07 11:00:00.000` in your timezone – Jaromanda X Aug 04 '19 at 01:13

1 Answers1

1

I found a solution. I replaced the space (" ") with a "&" in the client-side. Later, I did the opposite on the server-side.

client-side: str.replace(" ", "&")

server-side: str.replace("&", " ")

Dan
  • 31
  • 1
  • 5