注释“@FOS\RestBundle\Controller\Annotations\View”的未知键 "data"
Unknown key "data" for annotation "@FOS\RestBundle\Controller\Annotations\View"
我正在创建简单的 symfony api,并将 React 应用程序作为前端。它应该创建用户(仅用户名)和产品(名称和价格)。当我创建新用户时,它很好。但是,如果我决定以相同的方式创建新产品,则会出现错误
Unknown key "data" for annotation "@FOS\RestBundle\Controller\Annotations\View"
这是我在 ProductController
中的代码
/**
* Creates a new product entity.
* @Rest\Post("/post")
*/
function newAction(Request $request ) {
$product = new Product();
$body = $request->getContent();
$body = str_replace('"', '"', $body);
$body = json_decode($body, true);
$productName = $body['name'];
$productPrice = $body['price'];
$product->setName($productName);
$product->setPrice($productPrice);
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
$data['data'] = $product;
return new View($data,Response::HTTP_CREATED);
}
如果我对用户使用相同的逻辑和响应,它会起作用 fine.Here 是 UserController
中的代码
/**
* Creates a new user entity.
* @Rest\Post("/post")
*/
function newAction(Request $request ) {
$user = new User();
$body = $request->getContent();
$body = str_replace('"', '"', $body);
$body = json_decode($body, true);
$username = $body['username'];
$user->setUsername($username);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$data['data'] = $user;
return new View($data,Response::HTTP_CREATED);
}
现在我的问题 - 创建新用户就好了。新产品的创建中断。代码尽可能相似。知道为什么它不起作用吗?
[已解决] 这是我犯过的最愚蠢的错误之一。在使用 View
class 时,我已按 Alt+Enter 自动使用它。然而,似乎在 UserController
中它使用了正确的 View
class 而在 ProductController
中它使用了其他东西。
我正在创建简单的 symfony api,并将 React 应用程序作为前端。它应该创建用户(仅用户名)和产品(名称和价格)。当我创建新用户时,它很好。但是,如果我决定以相同的方式创建新产品,则会出现错误
Unknown key "data" for annotation "@FOS\RestBundle\Controller\Annotations\View"
这是我在 ProductController
/**
* Creates a new product entity.
* @Rest\Post("/post")
*/
function newAction(Request $request ) {
$product = new Product();
$body = $request->getContent();
$body = str_replace('"', '"', $body);
$body = json_decode($body, true);
$productName = $body['name'];
$productPrice = $body['price'];
$product->setName($productName);
$product->setPrice($productPrice);
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
$data['data'] = $product;
return new View($data,Response::HTTP_CREATED);
}
如果我对用户使用相同的逻辑和响应,它会起作用 fine.Here 是 UserController
/**
* Creates a new user entity.
* @Rest\Post("/post")
*/
function newAction(Request $request ) {
$user = new User();
$body = $request->getContent();
$body = str_replace('"', '"', $body);
$body = json_decode($body, true);
$username = $body['username'];
$user->setUsername($username);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$data['data'] = $user;
return new View($data,Response::HTTP_CREATED);
}
现在我的问题 - 创建新用户就好了。新产品的创建中断。代码尽可能相似。知道为什么它不起作用吗?
[已解决] 这是我犯过的最愚蠢的错误之一。在使用 View
class 时,我已按 Alt+Enter 自动使用它。然而,似乎在 UserController
中它使用了正确的 View
class 而在 ProductController
中它使用了其他东西。