无法对 class 的属性 "date" 值进行非规范化

Failed to denormalize attribute "date" value for class

我正在尝试通过 API 保存 React JS 中 Front 给出的对象

所以我在 Insomnia 中测试了这个对象:

{
"rate": 1.59,
"correction":"2 ui nOVORAPID",
"date": "2020-11-26",
"time": "7:30"}

我不明白为什么会出现错误

Failed to denormalize attribute "date" value for class "App\Entity\Bloodsugar": Expected argument of type "string", "object" given at property path "date".

我的控制器:

        $user = $this->getUser();

        $jsonReceived = $request->getContent();

        $json = json_decode($jsonReceived);

        $newBloodsugar = $serializer->deserialize($jsonReceived, BloodSugar::class, 'json');

...

我猜想 Symfony 无法识别日期格式“Y-m-d”,我该怎么做?

我猜你的 BloodSugar class 有无效的 setter 或 属性 类型。通常 symfony-serializer 将日期规范化为日期时间,而您的实体需要字符串。尝试将其更改为 DatetimeInterface,如下所示:

class BloodSugar {
    //..
    private ?DatetimeInterface $date;
    //..
    public function setDate(DatetimeInterface $date){
        $this->date = $date;
        return $this;
    }
    //..
}