我正在尝试在NativeScript
上构建移动应用程序,在这里我创建了一个具有login()
函数的授权类,该函数具有以下代码:
export default class NitsEditorAuth {
//Finding logged-in user.
isLoggedIn() {
return store.getters.access_token ? true : false;
}
//For Login user
login(user) {
const postData = {
grant_type: 'password',
username: user.email,
password: user.password,
client_id: clientId,
client_secret: clientSecret,
scope: '',
provider: provider
}
const authUser = {}
axios.post(authUrl, postData).then(response => {
if(response.status === 200)
{
authUser.access_token = response.data.access_token;
authUser.refresh_token = response.data.refresh_token;
axios.get(apiUrl + 'user/log', {headers: getHeader()}).then(response => {
if(response.status === 200){
authUser.email = response.data.email;
authUser.first_name = response.data.first_name;
authUser.last_name = response.data.last_name;
authUser.userRole = response.data.role;
store.commit('save', authUser);
return new Promise(function (resolve, reject) {
resolve('Login successful')
});
}
})
}
})
.catch((err) => {
if(err.response.status === 401){
// this.error = err.response.data.message
return new Promise(function (resolve, reject) {
reject('Validation error')
});
}
else
return new Promise(function (resolve, reject) {
reject('Something went wrong')
});
})
}
}
我在我的main.js
文件中包括了这样的类:
const nitsEditorAuth = new NitsEditorAuth();
Vue.prototype.$nitsEditorAuth = nitsEditorAuth;
我正在Vue-methods
内部调用此函数,例如:
login() {
this.$nitsEditorAuth
.login(this.user)
.then(() => {
this.processing = false;
this.$navigateTo(Home, { clearHistory: true });
})
.catch(() => {
this.processing = false;
this.alert(
"Unfortunately we could not find your account."
);
});
},
但是我在声明错误:
TypeError:无法读取未定义的属性“ then”。框架:功能:“登录”
帮我解决这个问题。谢谢。
答案 0 :(得分:3)
简单的解决方法是在函数外包装一个promise。并删除其余部分。 例如。
javascript node.js vue.js nativescript nativescript-vue
I'm trying to build a mobile application on NativeScript where I've created a class for authorization which has a login() function which has following codes:
export default class NitsEditorAuth {
//Finding logged-in user.
isLoggedIn() {
return store.getters.access_token ? true : false;
}
//For Login user
login(user) {
const postData = {
grant_type: 'password',
username: user.email,
password: user.password,
client_id: clientId,
client_secret: clientSecret,
scope: '',
provider: provider
}
return new Promise(function (resolve, reject) {
const authUser = {}
axios.post(authUrl, postData).then(response => {
if(response.status === 200)
{
authUser.access_token = response.data.access_token;
authUser.refresh_token = response.data.refresh_token;
axios.get(apiUrl + 'user/log', {headers: getHeader()}).then(response => {
if(response.status === 200){
authUser.email = response.data.email;
authUser.first_name = response.data.first_name;
authUser.last_name = response.data.last_name;
authUser.userRole = response.data.role;
store.commit('save', authUser);
resolve('Login successful')
}
})
}
})
.catch((err) => {
if(err.response.status === 401){
// this.error = err.response.data.message
reject('Validation error')
}
else
reject('Something went wrong')
})
})
}
答案 1 :(得分:2)
您通过不返回axios在您的login
方法(以及对axios.get(apiUrl + 'user/log',
的内部调用中返回的诺言)破坏了诺言链。
return axios.post(authUrl...
从then handlers of Promises are chained返回值,请参见以下示例:
// a here points to a promise that will resolve to "Just the 2 of us"
const a = Promise.resolve(1)
.then(val => val + val)
.then(val => `Just the ${val} of us`);
a.then(val => console.log(val))
答案 2 :(得分:1)
在登录功能中,您已在axios post调用中返回了一个promise,该调用本质上是异步的。您应该返回承诺的函数不返回任何东西。因此,您可以参考下面的代码。
login() {
let promise = new Promise(function(resolve, reject) {
// your post call (dummy data)
axios.post(authUrl, postData).then(response => {
if(response.status === 200) {
resolve('foo');
} else {
reject('Login successful');
}
});
return promise;
}
希望这会有所帮助。
答案 3 :(得分:1)
您可以使用异步/等待模式。
async login(user) {
const postData = {
grant_type: 'password',
username: user.email,
password: user.password,
client_id: clientId,
client_secret: clientSecret,
scope: '',
provider: provider
};
const authUser = {};
try {
const postResponse = await axios.post(authUrl, postData);
if (postResponse.status === 200) {
authUser.access_token = response.data.access_token;
authUser.refresh_token = response.data.refresh_token;
const response = await axios.get(apiUrl + 'user/log', {headers: getHeader()});
if (response.status === 200) {
authUser.email = response.data.email;
authUser.first_name = response.data.first_name;
authUser.last_name = response.data.last_name;
authUser.userRole = response.data.role;
store.commit('save', authUser);
return 'Login successful';
}
}
return 'Validation error';
}
catch(err) {
if (err.response.status === 401){
return 'Validation error';
}
return 'Something went wrong';
}
}