错误:ReferenceError:未定义getUnreadEmails

时间:2019-04-03 14:29:47

标签: javascript react-native

请在我在App.js中调用方法getNumberOfUnreadEmails时出现此错误: YellowBox.js:67可能的未处理的承诺拒绝(id:0): 错误:ReferenceError:未定义getUnreadEmails 错误:ReferenceError:未定义getUnreadEmails     在blob:http://localhost:8081/16f9ab34-0ee8-4ee2-8319-83df5fb4878a:102968:19     在tryCallOne(blob:http://localhost:8081/16f9ab34-0ee8-4ee2-8319-83df5fb4878a:31155:14)     在blob:http://localhost:8081/16f9ab34-0ee8-4ee2-8319-

期望: 给我未读邮件的数量

AzureInstance.js


 export default class AzureInstance {
constructor(credentials) {
this.authority = 'https://login.microsoftonline.com/common';
this.authorize_endpoint = '/oauth2/v2.0/authorize';
this.redirect_uri = 'https://login.microsoftonline.com/common/oauth2/nativeclient';
this.token_endpoint ='/oauth2/v2.0/token';
this.client_id = credentials.client_id;
this.client_secret = credentials.client_secret;
this.scope = credentials.scope;
this.token = {};

// function binding
this.getConfig = this.getConfig.bind(this);
this.setToken = this.setToken.bind(this);
this.getToken = this.getToken.bind(this);
this.getUserInfo = this.getUserInfo.bind(this);
this.getUnreadEmails=this.getUnreadEmails.bind(this);
this.getNumberOfUnreadEmails=this.getNumberOfUnreadEmails.bind(this);
//this.getRequest=this.getRequest.bind(this);
}

getConfig(){
return {
authority: this.authority,
authorize_endpoint: this.authorize_endpoint,
token_endpoint: this.token_endpoint,
client_id: this.client_id,
client_secret: this.client_secret,
redirect_uri: this.redirect_uri,
scope: this.scope,
}
}

setToken(token){
this.token = token;
}

getToken(){
return this.token;
}
getUserInfo()
{
try{
return this.getRequest("https://graph.microsoft.com/v1.0/me");
}
catch(err){
throw err;
}
}

getNumberOfUnreadEmails()
{
return getUnreadEmails().value.length();
}
getUnreadEmails()
{
try{
return this.getRequest("https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages?$filter=isRead ne true");
}
catch(err){
throw err;
}
}

getRequest(req) {
if (this.token === undefined){
throw new Error("Access token is undefined, please authenticate using Auth first");
}

return fetch(req, {
headers: {
'Authorization': "Bearer " + this.token.accessToken,
}
}).then(response => {
// return blob object
return response.json()
})
.then(response => {
// read blob object back to json
return response
}).catch(err => {
// incase of error reject promise
throw new Error(err);
});
}
}

App.js


import React from 'react';
import {AppRegistry,Text,StyleSheet,Picker} from 'react-native';
import {View} from 'react-native-webview';

import {AzureInstance, AzureLoginView} from '../src/screens/auth/';

// CONSTANT
const CREDENTIAILS = {
client_id: '--------------------------',
client_secret: '--------------------',
scope: 'User.ReadBasic.All Mail.Read offline_access'
};

export default class azureAuth extends React.Component {
  constructor(props){
    super(props);
this.state = {
displayName : "Nan",
mail : "",
id : ""
};
    this.azureInstance = new AzureInstance(CREDENTIAILS);
this._onLoginSuccess = this._onLoginSuccess.bind(this);
  }

  _onLoginSuccess(){
    this.azureInstance.getUserInfo().then(result => {
console.log(result);
    }).catch(err => {
      console.log(err);
});
this.azureInstance.getUnreadEmails().then(result =>{
console.log(result);
}).catch(err =>{
console.log(err);
});

this.azureInstance.getNumberOfUnreadEmails().then(result =>{
console.log(result);
}).catch(err =>{
console.log(err);
});
  }

render() {
return (
<AzureLoginView
azureInstance={this.azureInstance}
loadingMessage="Requesting access token"
onSuccess={this._onLoginSuccess}
/>
);
}
}

const styles = StyleSheet.create({
container: {
paddingTop: 23
},
baseText: {
fontFamily: 'Cochin',
},
titleText: {
fontSize: 20,
fontWeight: 'bold',
},
});

AppRegistry.registerComponent('azureAuth', () => azureAuth);

2 个答案:

答案 0 :(得分:1)

我认为应该是Parcelable

this.getUnreadEmails()

让我知道它是否有效!

答案 1 :(得分:0)

getNumberOfUnreadEmails()
{
return this.getUnreadEmails().value.length();
}

您刚刚忘记添加此内容。方法调用之前 此外,您还可以使用以下语法:

getNumberOfUnreadEmails = () => this.getUnreadEmails().value.length();

它不仅可以缩短代码,而且在这种情况下还可以删除绑定