I'm trying to use the hashids plugin to hash ticket_id
within a for loop.
for (var i = 0; i < result.length; i++) {
var hashids = new Hashids("", 5);
var id = hashids.encode(result[i].ticket_id);
console.log(hashids);
[...]
html += '<tr class="ticketClick" data-url="' + baseURL + id>' [...]
}
HTML for a table is dynamically generated based on the result
passed by an ajax call. When the table row is clicked I redirect to the url in the data-url attribute.
The problem is that hashids doesn't run and therefore id
isn't being appended to the url.
The console log of hashids
produces:
Hashids {version: "1.0.2", minAlphabetLength: 16, sepDiv: 3.5, guardDiv: 12, errorAlphabetLength: "error: alphabet must contain at least X unique characters"…}
I've searched for the error and didn't find anything...
What I find strange is that if I pass an integer directly into the hashids method like:
var id = hashids.encode(20);
The encode works and returns the expected hash.
A console.log of result[i].ticket_id
returns the expected integer, so the for loop is doing it's job properly. Declaring new Hashids()
outside the for
loop doesn't seem to make a difference. So I'm not sure what's going wrong here. Any suggestions?
The problem was caused because of the Ajax function pulling the data in JSON format.
Parse the value to an integer:
parseId(result[i].ticket_id)