Programing

Vuex 디스패치가 반환되지 않음

c10106 2022. 5. 2. 21:32
반응형

Vuex 디스패치가 반환되지 않음

다음 코드를 가지고 있다.

signin(context, payload, resolve) {
    console.log("SIGNIN action")

    const aemail = payload.locmail
    const apassw = payload.locpass

    backend.get("api/auth/signin", {
      headers: {
        'planck': 'FRESH'
      },
      crossDomain: true,
      params: {
        password: apassw,
        email: aemail
      }
    }).then(function(ret) {
      console.log("SENT")
      if (ret.data.auth === 'TRUE') {
        context.commit('authenticate', ret.data.planck)
        state.isAuthenticated = true
      } else {
        state.isAuthenticated = false
      }
      console.log(ret)
      return Promise.resolve(ret)
    });
  }

구성 요소에서 호출하면:

this.$store.dispatch('signin', {
        locmail,
        locpass
      }).then(ret => {
        console.log(ret);
      });

그런 다음 콘솔 로그 인쇄undefined내가 여기서 뭘 잘못하고 있는 거지?설명서에서 나는 다음을 사용해야 한다고 읽었다.resolve()하지만 그게 함수가 아니라는 오류를 범하게 돼

리턴 더backend을 약속하다signin액션

  signin(context, payload) {
    // ...

    // The action needs to return a Promise
    return backend.get("api/auth/signin", {
      /* ...*/
    }).then(function(ret) {
      /* ... */
    });
  }

또, 변신을 하고 있는 것 같다.state행동의 대상이며 이는 돌연변이로 제한되어야 한다.

개발 중에 엄격모드를 사용하고 있다면 이에 대한 경고를 볼 수 있을 것이다.

너는 그 약속을 지키지 않을 것이다.변경하기signin()@Emile Bergeron이 제안한 대로 여기에 기능한다.

signin(context, payload) {
    console.log("SIGNIN action")

    const aemail = payload.locmail
    const apassw = payload.locpass

    return backend.get("api/auth/signin", {
      headers: {
        'planck': 'FRESH'
      },
      crossDomain: true,
      params: {
        password: apassw,
        email: aemail
      }
    }).then(function(ret) {
      console.log("SENT")
      if (ret.data.auth === 'TRUE') {
        context.commit('authenticate', ret.data.planck)
        state.isAuthenticated = true
      } else {
        state.isAuthenticated = false
      }
      console.log(ret)
      return Promise.resolve(ret)
    });
  }

참조URL: https://stackoverflow.com/questions/49710481/vuex-dispatch-doesnt-return

반응형