【发布时间】:2023-04-09 08:21:02
【问题描述】:
我可能有点愚蠢 :) 但是我已经复制粘贴了数百个解决方案,并尝试了我能想到的所有方法(我对 VUE 和 JS 完全陌生,所以请善待:))
这是我现在拥有的代码。我可以看到它在 F12 中获取数据,但我无法将数据放入可以在函数外部使用的变量中。
我想对我的数据做一些 reponseDataToSession.user.name 等等,或者循环遍历一些数据,但是如果我在我的提取之外使用 console.log,我会得到空或什么都没有。
请帮助我让我的代码在这个小东西上花费一整天。
我得到的错误是:ReferenceError: reponseDataToSession is not defined
这里是代码。
<template>
<div id="app">
<input
v-model="sessionUUID"
@keyup.enter="fetchsessions()"
name="search"
type="text"
/>
<input
@mouseup="fetchsessions()"
name="serchSubmit"
value="SÖK"
type="submit"
/>
<template v-if="viewData">
{{responseDataToSession}}
</template>
</div>
</template>
<script>
import base64 from "base-64";
export default {
name: "App",
data() {
return {
sessionUUID: "",
responseDataToSession: {},
viewData: false,
};
},
methods: {
fetchsessions: function () {
const url = "http://localhost:8080/gethistory";
const method = "POST"
const username = "user";
const password = "bass";
const body = JSON.stringify({"uuid": "' + this.sessionUUID + '","answers": []});
const headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("Authorization", "Basic " + base64.encode(username + ":" + password));
this.responseDataToSession = fetching();
console.log("1"+this.reponseDataToSession)
this.viewData = true
async function fetching() {
return this.responseDataToSession = await fetch(url, { method, headers, body });
}
}
}
};
</script>
【问题讨论】:
-
console.log(reponseDataToSession.sessionSteeps)应该是console.log(this.reponseDataToSession.sessionSteeps) -
无关:
reponseDataToSession在data()中未正确初始化 -
更改 console.log,现在我得到 Uncaught (in promise) ReferenceError: response is not defined。关于初始化,请告诉我该怎么做,就像我说我对这一切都很陌生。
标签: vue.js async-await fetch undefined