英文:
Email value won't pass through from vue to springboot
问题
以下是翻译好的部分:
"I have made a api that should get a user by their email but I get an error."
我创建了一个API,应该通过电子邮件获取用户,但是我遇到了错误。
"I have similar api that just works, I think it goes wrong when I send it to the controller."
我有一个类似的API,它正常工作,我认为问题出在将它发送到控制器时。
"The error:"
错误信息:
"Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'email' for method parameter type String is not present]"
已解决 [org.springframework.web.bind.MissingServletRequestParameterException: 方法参数类型为String的必需请求参数 'email' 不存在]
"My vue code:"
我的Vue代码:
"Invite player by email
"
通过电子邮件邀请玩家
"data() {"
数据() {
"return {"
返回 {
"invitedPlayer: ""
invitedPlayer: ""
"computed: {"
计算属性: {
"email() {"
email() {
"return this.invitedPlayer;"
return this.invitedPlayer;
"const authService = new AuthService();"
const authService = new AuthService();
"authService.getUserByEmail(this.email)"
authService.getUserByEmail(this.email)
"Authservice"
Authservice
"getUserByEmail(email) {"
getUserByEmail(email) {
"return api.get("/auth/get", {"
return api.get("/auth/get", {
"email"
email
"Controller"
控制器
"@GetMapping("/get")"
@GetMapping("/get")
"public ResponseEntity<Optional
public ResponseEntity<Optional
"Optional
Optional
"if (user == null) {"
if (user == null) {
"return new ResponseEntity<>(HttpStatus.NOT_FOUND);"
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
"}"
}
"return new ResponseEntity<>(user, HttpStatus.OK);"
return new ResponseEntity<>(user, HttpStatus.OK);
"UserRepository"
UserRepository
"public interface UserRepository extends JpaRepository<User, Long> {"
public interface UserRepository extends JpaRepository<User, Long> {
"Optional
Optional
"Optional
Optional
英文:
I have made a api that should get a user by their email but I get an error.
I have similar api that just works, I think it goes wrong when I send it to the controller.
The error:
Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'email' for method parameter type String is not present]
My vue code:
<div class="settingBlock">
Invite player by email <br>
<input v-model="invitedPlayer" id="invitedPlayer" type="text" class="field" required placeholder="Email" />
</div>
data() {
return {
invitedPlayer: ""
}
},
computed: {
email() {
return this.invitedPlayer;
}
},
const authService = new AuthService();
authService.getUserByEmail(this.email)
Authservice
getUserByEmail(email) {
return api.get("/auth/get", {
email
})
}
Controller
@GetMapping("/get")
public ResponseEntity<Optional<User>> getUserByEmail(@RequestParam String email) {
Optional<User> user = userRepository.findByEmail(email);
if (user == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(user, HttpStatus.OK);
}
UserRepository
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByUsername(String username);
Optional<User> findByEmail(String email);
答案1
得分: 1
不确定在Vue上下文中api
是什么,但我猜你需要将Authservice
更改为:
getUserByEmail(email) {
return api.get("/auth/get?email=" + email);
}
英文:
Not sure what api
is here in the Vue context but I would guess that you need to change the Authservice to
getUserByEmail(email) {
return api.get("/auth/get?email=" + email)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论