单击保存按钮时,我试图更改用户个人资料图片,我的模板如下所示:
<q-file
v-model="file"
label="Pick one file"
class="text-center"
use-chips
accept=".jpg, image/*"
style="width: 200px"
/>
<q-btn flat class="text-primary" @click="saveUsersProfile(file)">
Save
</q-btn>
在我的脚本部分
<script>
import { mapActions } from "vuex";
export default {
name: "profile",
data: () => ({
file: null
}),
computed: {
...mapActions("store", ["saveUsersProfile"])
},
Store.js
//Let users change Profile Picture
saveUsersProfile({}, payload) {
// Get the File
const file = payload[0];
const extension = file.name.split(".").pop();
const uploadDate = new Date();
const fileName = `${firebaseAuth.currentUser.uid}-${uploadDate.toDateString()}.${extension}`;
firebaseStorage
.ref(`users/profile/${fileName}`)
.put(file)
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
});
控制台错误:
Error in v-on handler: "TypeError: Cannot read property 'name' of undefined"
vue.runtime.esm.js?2b0e:1888 TypeError: Cannot read property 'name' of undefined
at Store.saveUsersProfile (store.js?07a4:122)
我不知道为什么无法获得文件扩展名。请帮助。
{}
)。因此,payload[0]
等于undefined
。尝试调用.name
时,它将引发此错误。您需要map您的操作。
来自here:
mapActions将只是地图动作。
它会为您创建简写,并且您像调用其他任何普通方法一样调用这些方法。在...mapActions('auth', ['login'])
之后>
this.$store.dispatch('auth/login', yourPayload)
等于
this.login(yourPayload)
在您的情况下,您必须在脚本会话中添加方法:
<script>
import { mapActions } from "vuex";
export default {
name: "profile",
data: () => ({
file: null
}),
methods: {
...mapActions("store", ["saveUsersProfile"]),
saveUsersProfile({name}),
},
对于商店:
//Let users change Profile Picture saveUsersProfile({}, payload) { // Get the File const file = payload; // I don't understand why you need [0], but maybe I'm wrong const extension = file.name.split(".").pop(); ...
祝你好运