Vue集成Appwrite登录报错:TypeError: account.createEmailSession is not a function
问题描述
按照Appwrite的Vue教程开发,注册功能正常,可在Appwrite控制台Auth模块创建新用户,但点击登录按钮时抛出错误:
Uncaught (in promise) TypeError: account.createEmailSession is not a function
相关代码如下:
src/appwrite/index.js
import { Client, Databases, Account } from "appwrite"; const client = new Client(); client .setEndpoint("https://cloud.appwrite.io/v1") .setProject("PROJECT-ID"); // 替换为你的项目ID export const account = new Account(client); export const databases = new Databases(client);
src/store/user.js
import { ID } from 'appwrite'; import { account } from "@/appwrite"; import { reactive } from "vue"; export const user = reactive({ current: null, async init() { try { this.current = await account.get(); } catch (e) { this.current = null; } }, async register(email, password) { await account.create(ID.unique(), email, password); await this.login(email, password); }, async login(email, password) { await account.createEmailSession(email, password); window.location.href = "/"; }, async logout() { await account.deleteSession("current"); this.current = null; }, });
src/pages/auth/Login.vue
<script setup> import { user } from "@/store/users"; import { ref } from "vue"; const email = ref(""); const password = ref(""); </script> <template> <section> <h1>登录或注册</h1> <form> <input type="email" placeholder="邮箱" v-model="email" /> <input type="password" placeholder="密码" v-model="password" /> <div> <button type="button" @click="user.login(email, password)"> 登录 </button> <button type="button" @click="user.register(email, password)"> 注册 </button> </div> </form> </section> </template>
问题原因
这是Appwrite SDK版本迭代导致的方法名变更:
- 在Appwrite SDK 0.x版本中,邮箱登录的方法是
createEmailSession - 升级到1.x及以上版本后,该方法被重命名为
createSession,原方法被移除
注册功能正常是因为account.create()方法在新版本中仍被保留(兼容旧写法),但登录用的旧方法已不存在,因此抛出"不是函数"的错误。
解决方法
将src/store/user.js中login方法里的account.createEmailSession替换为account.createSession即可:
修改后的login方法:
async login(email, password) { await account.createSession(email, password); window.location.href = "/"; }
同时建议检查项目中安装的Appwrite SDK版本,确保后续使用的方法与版本匹配。
内容的提问来源于stack exchange,提问作者user16806454
相关产品推荐
相关产品推荐

