The API is made using Nodejs Express. This Hindi API is live, showing weird character for Hindi language. It's English version displays English results perfectly. Below is the code for Hindi API:
app.get("/api/find/hindi/:find",function(request, response)
{
let db = new sqlite3.Database("./quranDb.db",(err) => {
if (err){
console.log("Not connected to sqlite")
}
else{
console.log("Connected to sqlite")
}
});
let sql = `SELECT Surat_ID, Ayat_ID, Surat_Name, Hindi FROM QuranTest`;
db.all(sql, [], (err, rows) => {
if (err) {
throw err;
}
rows.forEach((row) => {
ayats.push(JSON.stringify({Translation: row.Hindi,SuratName: row.Surat_Name,SuratID: row.Surat_ID,AyatNo: row.Ayat_ID}));
});
//console.log(ayats);
Translation="";
Surat_No="";
Surah_Name="";
Ayat_No="";
try {
ayats.forEach(function(element) {
if (element.toLowerCase().includes(request.params.find.toLowerCase())===true)
{
counting++;
element=JSON.parse(element);
Surah_Name = element.SuratName;
Ayat_No = element.AyatNo;
Surah_No = element.SuratID
Translation = "In Surah "+ element.SuratName+", Ayat Number: "+element.AyatNo+", Quran says: "+ element.Translation;
const tempObj={
Surah_No,
Surah_Name,
Ayat_No,
Translation
}
parentObj[`result_${counting}`]=tempObj
}
if (counting===10){
throw BreakException
}
})
} catch(e) {
if (e!==BreakException) throw e
}
if (counting ===0){
response.write(JSON.stringify({"speech":"No results found"}))
}
else{
response.write(JSON.stringify(parentObj))
}
response.send();
counting = 0;
parentObj={};
});
empty();
function empty() {
ayats.length = 0;
}
db.close((err) => {
if (err) {
return console.error(err.message);
}
console.log('Close the database connection.');
});
})
Hindi API
As it can be seen, value of translation
field is gibberish. It should be in hindi format. The SQL database doesn't have any issue. It shows perfect hindi.
I also have tried
...
}
else{
response.write(JSON.stringify(parentObj))
}
response.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});
response.send();
counting = 0;
...
but it stops the hindi api from executing. Browser returns an error to refresh the page etc.