【问题标题】:Nodejs Express App shows weird characters for hindi, querying over Sqlite databaseNodejs Express App 显示奇怪的印地语字符,查询 Sqlite 数据库
【发布时间】:2019-10-29 00:32:24
【问题描述】:

API 是使用 Nodejs Express 制作的。这个Hindi API 是现场直播,显示印地语的奇怪字符。 English version 完美显示英文结果。以下是印地语 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.');
  });

})

印地语 API

可以看出,translation 字段的值是乱码。它应该是印地语格式。 SQL 数据库没有任何问题。它显示了完美的印地语。

英文 API

我也试过

...

}


  else{
    response.write(JSON.stringify(parentObj))
  }
  response.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});
  response.send();
  counting = 0;
...

但它会阻止印地语 api 执行。浏览器返回错误刷新页面等。

【问题讨论】:

    标签: node.js sqlite express utf-8 hindi


    【解决方案1】:

    我猜对了。我的错误是,只告诉标题关于 utf-8 编码是不够的。 response.write(JSON.stringify(parentObj),"utf-8") 还需要知道正文应该遵循哪种编码。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多