簡體   English   中英

賦值不是表達式,在這個上下文中只允許表達式 Spring Boot Kotllin

[英]Assignments are not expressions, and only expressions are allowed in this context Spring Boot Kotllin

我在我的應用程序中使用 Kotlin 和 Springboot。 我想在 lambda 函數旁邊設置 problem.problemId=patient.patientId 但 kotlin 給出錯誤作為響應。 我得到以下類型的錯誤

Assignments are not expressions, and only expressions are allowed in this context Spring Boot Kotllin

患者服務.kt

package com.nillmani.hospitalmanagement.service

import com.nillmani.hospitalmanagement.entity.Patient
import com.nillmani.hospitalmanagement.exception.PatientNotFoundException
import com.nillmani.hospitalmanagement.model.ReqPatient
import com.nillmani.hospitalmanagement.repository.PatientRepository
import org.modelmapper.ModelMapper
import org.slf4j.Logger
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*
import java.util.function.Consumer


@Service
class PatientService {
    @Autowired
    private lateinit var patientRepository: PatientRepository
    private lateinit var modelMapper : ModelMapper
    private lateinit var logger: Logger

    fun PatientService(patientRepository: PatientRepository, modelMapper: ModelMapper, logger: Logger) {
        this.patientRepository = patientRepository
        this.modelMapper = modelMapper
        this.logger = logger
    }

    @Throws(Exception::class)
     fun findAll():List<ReqPatient>{
        try {
            var patient : List<Patient?>? = patientRepository.findAllByStatusEquelsOne()
            if (patient != null) {
                if (patient.size < 1){
                    logger.error("Here No patient Record available")
                    throw PatientNotFoundException()
                }
            }
            val dto : ReqPatient = modelMapper.map(patient,ReqPatient::class.java)
            val patientDto  : List<ReqPatient> = listOf(dto)
            patientDto.forEach{patient ->(patient.problems).forEach { problem ->(problem.problemId=patient.patientId ) }}
            return listOf(dto)
        }catch (e:Exception){
            throw Exception(e)
        }
     }
}

我在這一行出錯

val patientDto  : List<ReqPatient> = listOf(dto)
            patientDto.forEach{patient ->(patient.problems).forEach { problem ->(problem.problemId=patient.patientId ) }}

如果我使用賦值運算符 kotlin 給出異常,我如何將 PatientId 分配給 ProblemID

您需要刪除作業周圍的括號

val patientDto  : List<ReqPatient> = listOf(dto)
patientDto.forEach{patient -> 
    (patient.problems).forEach { problem -> problem.problemId = patient.patientId  }
}

在 kotlin 中,您可以將表達式括在括號中以創建括號內的表達式 因為賦值不是表達式,所以你會得到錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM