2

This is the result I am getting from my database when I do a query:

[
    {
        "name": "file1.txt",
        "id": 1,
        "file_path": "./public/files/file1.txt"
    }
    {
        "name": "file3.txt",
        "id": 3,
        "file_path": "./public/files/file3.txt"
    },
    {
        "name": "file4.txt",
        "id": 8,
        "file_path": "./public/files/file4.txt"
    },
    {
        "name": "file5.txt",
        "id": 7,
        "file_path": "./public/files/file5.txt"
    }
]

And what I want to print is this kinda of format of file path ./public/files/file1.txt

This is what I am trying right now but it doesn't seem to work:

var paths = [];
if(result.length > 0) {
    var tmp_path = result[0].file_path

    var count = 0

    result.forEach(element =>  {
        count++
        tmp_path = element.file_path
        paths.push(decodeURI(tmp_path))
    })
    console.log(JSON.stringify(paths,null,4))
}

The result I get is this:

[
"./public/files/file1.txt",
"./public/files/file3.txt",
"./public/files/file4.txt",
"./public/files/file5.txt",
]

Isn't decodeURI() the function I am supposed to use in this case?

I am working on Node.js with router and ejs npms. So this is part of a get function and the result will be populated in front-end with ejs. So what I am trying to do is to pass the correct format on the result which is PATHS

Gerasimos Ragavanis
  • 361
  • 1
  • 3
  • 16

4 Answers4

1

You can use decodeURI() in the following way

var uri = "./public/files/file1.txt";
var enc = encodeURI(uri);
var dec = decodeURI(enc);

You can show this URL in html like below:

document.getElementById("demo").innerHTML = dec;
bpjoshi
  • 1,091
  • 16
  • 23
1

You can tweak your code so that that file path is saved as a HTML content in a element say, div then get the HTML from this element to get the decoded value and push it into the array.

var result = [
    {
        "name": "1k",
        "id": 1,
        "file_path": "./public/files/file1.txt"
    },
    {
        "name": "3k",
        "id": 3,
        "file_path": "./public/files/file3.txt"
    },
    {
        "name": "4k",
        "id": 8,
        "file_path": "./public/files/file4.txt"
    },
    {
        "name": "5k",
        "id": 7,
        "file_path": "./public/files/file5.txt"
    }
];

var paths = [];
if(result.length > 0) {
    var tmp_path = result[0].file_path
    var count = 0
    
    result.forEach(element =>  {
        count++;
        tmp_path = element.file_path;
        var elem = document.createElement('div');
        elem.innerHTML = tmp_path;
        paths.push(elem.innerHTML);
    })
    console.log(JSON.stringify(paths,null,4))
}
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
1

Not sure if this would work for you, but you can use a .map to remap the array into an array of strings making slight adjustments:

So for each file_path split with ';' then take the last element of the split 'filei.text' and concatenate './public/files/' before it.

const res = data.map(e => {
  const path = e.file_path.split(';');
  const fileName = path[path.length-1];
  return './public/files/' + fileName;
});

const data = [
    {
        "name": "1k",
        "id": 1,
        "file_path": "./public/files/file1.txt"
    },
    {
        "name": "3k",
        "id": 3,
        "file_path": "./public/files/file3.txt"
    },
    {
        "name": "4k",
        "id": 8,
        "file_path": "./public/files/file4.txt"
    },
    {
        "name": "5k",
        "id": 7,
        "file_path": "./public/files/file5.txt"
    }
];

const res = data.map(e => {
  const path = e.file_path.split(';');
  const fileName = path[path.length-1];
  return './public/files/' + fileName;
});

console.log(res)
Ivan
  • 34,531
  • 8
  • 55
  • 100
0

just encode the file_path with encodeURI you will get original url e.g.:

var uri = "./public/files/file1.txt";
console.log(encodeURI(uri));

Will return:

"./public/files/file1.txt"
Ivan
  • 34,531
  • 8
  • 55
  • 100
Lavanya E
  • 206
  • 2
  • 12