Skip to content
Advertisement

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. 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);

Advertisement

Answer

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)
  }
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement