Javascript 错误:ReferenceError:未定义getUnreadEmails
在App.js中调用getNumberOfUnderemails方法时,请注意,我有以下错误: YellowBox.js:67可能的未处理承诺拒绝(id:0): 错误:ReferenceError:未定义getUnreadEmails 错误:ReferenceError:未定义getUnreadEmails 在blob: 在TryCalone(blob:) 在blob: 期望: 告诉我未读邮件的数量 AzureInstance.jsJavascript 错误:ReferenceError:未定义getUnreadEmails,javascript,react-native,Javascript,React Native,在App.js中调用getNumberOfUnderemails方法时,请注意,我有以下错误: YellowBox.js:67可能的未处理承诺拒绝(id:0): 错误:ReferenceError:未定义getUnreadEmails 错误:ReferenceError:未定义getUnreadEmails 在blob: 在TryCalone(blob:) 在blob: 期望: 告诉我未读邮件的数量 AzureInstance.js export default class AzureIns
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);
从“React”导入React;
从“react native”导入{AppRegistry,Text,StyleSheet,Picker};
从“react native webview”导入{View};
从“../src/screens/auth/”导入{AzureInstance,AzureLoginView};
//不变的
施工证书={
客户id:“------------------------------------”,
客户_机密:'-------------',
作用域:“User.ReadBasic.All Mail.Read offline\u access”
};
导出默认类azureAuth扩展React.Component{
建造师(道具){
超级(道具);
此.state={
显示名称:“南”,
邮件:“,
id:“
};
this.azureInstance=新的azureInstance(凭证);
this.\u onloginsucess=this.\u onloginsucess.bind(this);
}
_onloginsucess(){
这个.azureInstance.getUserInfo().then(结果=>{
控制台日志(结果);
}).catch(错误=>{
控制台日志(err);
});
this.azureInstance.getUnreadEmails().then(结果=>{
控制台日志(结果);
}).catch(错误=>{
控制台日志(err);
});
this.azureInstance.GetNumberOfUnderemails().then(结果=>{
控制台日志(结果);
}).catch(错误=>{
控制台日志(err);
});
}
render(){
返回(
);
}
}
const styles=StyleSheet.create({
容器:{
加油站:23
},
基本文本:{
fontFamily:“科钦”,
},
标题文字:{
尺寸:20,
fontWeight:'粗体',
},
});
AppRegistry.registerComponent('azureAuth',()=>azureAuth);
我认为应该是这个.getUnreadEmails()
让我知道它是否有效
getNumberOfUnreadEmails()
{
return this.getUnreadEmails().value.length();
}
你刚刚忘了加上这个。在方法调用之前
此外,还可以使用以下语法:
getNumberOfUnreadEmails = () => this.getUnreadEmails().value.length();
它不仅缩短了您的代码,而且您还可以在这种情况下删除绑定谢谢,请您解释一下为什么我可以在这种情况下删除绑定?我建议您阅读以下文章中的“主要优点:不绑定‘this’”:如果您仍然有一些问题,请询问一个更有用的链接:
getNumberOfUnreadEmails = () => this.getUnreadEmails().value.length();