api利用時にエラーはで出ないのに特定の行以降の処理が進まない

Vue.jsである行を境にそれ以降の処理が進まない

もしくは何も起こらない

しかも、なぜかエラーは出ない場合

 

axiosの後に改行が入っている?

[code lang="javascript"] axios
.post("/api/customers/create", postData)
.then((response) => {
console.log(response);
this.centerSnackbar.text = postData.name + "を新規登録しました。";
this.centerSnackbar.snackbar = true; //スナックバーを表示
this.$router.go({//リロード
path: this.$router.currentRoute.path,
force: true,
});
})
[/code]

全然リロードも走らんしなんやこれってなった。

axiosと.postの間は改行してはいけなかった(?)

[code lang="javascript"] axios.post("/api/customers/create", postData)
.then((response) => {
console.log(response);
this.centerSnackbar.text = postData.name + "を新規登録しました。";
this.centerSnackbar.snackbar = true; //スナックバーを表示
this.$router.go({//リロード
path: this.$router.currentRoute.path,
force: true,
});
})
[/code]

これで解決。

 

なんか改行してても動く場合もあるしよくわからん。

とりあえずaxiosの直後は改行しないほうが無難。する意味もないし。

 

apiのパスの先頭にスラッシュが抜けてる

apiのパスの先頭にスラッシュが抜けている場合も修正が必要。

[code lang="javascript"] axios.get("api/loginCheck")
.then(response => {
this.isLoggedIn = true
console.log(response)
})
.catch(error => {
console.log(error)
this.isLoggedIn = false
this.$store.commit("message",'ログインしてください。')
this.$router.push("/login") //ログイン画面にジャンプ
})
[/code]

 

1行目「api/loginCheck」を「/api/loginCheck」に修正

[code lang="javascript"] axios.get("/api/loginCheck")
.then(response => {
this.isLoggedIn = true
console.log(response)
})
.catch(error => {
console.log(error)
this.isLoggedIn = false
this.$store.commit("message",'ログインしてください。')
this.$router.push("/login") //ログイン画面にジャンプ
})
[/code]

これで解決。

これも先頭にスラッシュなくても動いてしまうときがあるけど、原則入れておこう。

無制限に質問可能なプログラミングスクール!

万が一転職できない場合は、転職保障全額返金できるコースもあり!!

無制限のメンター質問対応

 

DMMウェブキャンプでプログラミングを学習しませんか?

独学より成長スピードをブーストさせましょう!

 

まずは無料相談から!

おすすめの記事