1

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. enter image description here

English API enter image description here

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.

Muhammad Naufil
  • 2,420
  • 2
  • 17
  • 48

1 Answers1

0

I got it right. My mistake was, only telling the header about utf-8 encoding is not enough. response.write(JSON.stringify(parentObj),"utf-8") also needs to know, which encoding the body should follow.

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")
        }
    });
    response.writeHead(200, {"Content-Type": "text/html; charset=utf-8"}); 
    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"}),"utf-8")
    }  
  else{
    response.write(JSON.stringify(parentObj),"utf-8")
  }

  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.');
  });

})

enter image description here

Muhammad Naufil
  • 2,420
  • 2
  • 17
  • 48