如何使用Circe从Doobie Scala PostgreSQL数据库中读取JSON?

问题描述

我尝试创建以下隐式对象,以便可以从postgreSQL数据库获取/读取数据。我尝试添加推荐的隐式变量,但是它们变成灰色并且似乎没有被使用。

      exports.viewTimings = async (req,res) => {
        console.log(typeof req.body.date); //string
        console.log(moment(req.body.date).format("LL")); // September 2,2020
        console.log(typeof req.body.date); //string

        const timings = await DoctorSlot.find({
          doctor: req.user.jwt_payload.user._id,});
        if (timings.length < 1) return res.json("No slots for today.");

        timings.forEach((element) => {
          console.log(typeof element.dateOfAppointment.toDateString()); // made object to string
          console.log(element.dateOfAppointment.toDateString());  // Wed Sep 02 2020

          if (element.dateOfAppointment.toDateString() === moment(req.body.date).format("LL"))
            //Wed Sep 02 2020 === September 2,2020
            console.log("work");
        });
        res.json(timings);
        // return res.json(timings);
      };
implicit val get: Get[JobPostDetails] =
 Get[Json].temap(_.as[JobPostDetails].leftMap(_.show))

  def createTable: doobie.Update0 = {
    sql"""
         |CREATE TABLE IF NOT EXISTS jobs (
         |  id TEXT PRIMARY KEY,|  details JSON NOT NULL
         |)
       """.stripMargin
      .update
  }

case class JobPost(id: String,details: JobPostDetails)

case class JobPostDetails(title: String,description: String,salary: Double,employmentType: String,employer: String)

[warn] insecure HTTP request is deprecated 'http://repo.typesafe.com/typesafe/releases/'; switch to HTTPS or opt-in as ("Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/").withAllowInsecureProtocol(true)
[info] Compiling 1 Scala source to /Users/ryanmcavoy/fullStackRyan/job-board/target/scala-2.13/classes ...
[error] /Users/ryanmcavoy/fullStackRyan/job-board/src/main/scala/io/github/jobboard/model/JobPost.scala:31:44: value leftMap is not a member of io.circe.Decoder.Result[io.github.jobboard.model.JobPostDetails]
[error]       Get[Json].temap(_.as[JobPostDetails].leftMap(_.show))
[error]                                            ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 1 s,completed 3 Sep 2020,16:41:02
sbt:job-board> 


解决方法

较旧版本的Scala为.leftMap提供了Either.left(因为这是Circe Result别名),在您使用的源代码中可能已经提到过。

但是,较新的版本稍微清理了API,因为它们使用.right.leftMap来聚合许多方法。因此,.left.map变成了.left.flatMap,但是您也有了Either等,因此,不仅可以在符合右偏的用例中轻松使用.leftMap。 / p>

长话短说-在从未使用过的Scala版本中,将.left.map替换为 SQLserver_settings = {'server': 'servername','database': 'database','username': 'username','password': 'password','driver': '{ODBC Driver 17 for SQL Server}'} cnxn = pyodbc.connect('DRIVER=' + SQLserver_settings['driver'] + ';SERVER=' + SQLserver_settings['server'] + ';PORT=1433;DATABASE=' + SQLserver_settings['database'] + ';UID=' + SQLserver_settings['username'] +';PWD=' + SQLserver_settings['password'])

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...