Email value won’t pass through from vue to springboot.

huangapple go评论63阅读模式
英文:

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> getUserByEmail(@RequestParam String email) {"
public ResponseEntity<Optional> getUserByEmail(@RequestParam String email) {

"Optional user = userRepository.findByEmail(email);"
Optional user = userRepository.findByEmail(email);

"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 findByUsername(String username);"
Optional findByUsername(String username);

"Optional findByEmail(String email);"
Optional findByEmail(String email);

英文:

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 &#39;email&#39; for method parameter type String is not present]

My vue code:

&lt;div class=&quot;settingBlock&quot;&gt;
        Invite player by email &lt;br&gt;
        &lt;input v-model=&quot;invitedPlayer&quot; id=&quot;invitedPlayer&quot; type=&quot;text&quot; class=&quot;field&quot; required  placeholder=&quot;Email&quot; /&gt;
      &lt;/div&gt;

    data() {
        return {
          invitedPlayer: &quot;&quot;
        }
      },
computed: {
    email() {
      return this.invitedPlayer;
    }
  },

const authService = new AuthService();
  authService.getUserByEmail(this.email)

Authservice

getUserByEmail(email) {
    return api.get(&quot;/auth/get&quot;, {
      email
    })
  }

Controller

    @GetMapping(&quot;/get&quot;)
  public ResponseEntity&lt;Optional&lt;User&gt;&gt; getUserByEmail(@RequestParam String email) {
    Optional&lt;User&gt; user = userRepository.findByEmail(email);
    if (user == null) {
      return new ResponseEntity&lt;&gt;(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity&lt;&gt;(user, HttpStatus.OK);
  }

UserRepository

    public interface UserRepository extends JpaRepository&lt;User, Long&gt; {
  Optional&lt;User&gt; findByUsername(String username);

  Optional&lt;User&gt; 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(&quot;/auth/get?email=&quot; + email)
  }

huangapple
  • 本文由 发表于 2023年2月8日 22:21:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定