无法读取 reactjs 的未定义 n 获取方法的 属性 'setState'
Cannot read property 'setState' of undefined n fetch method of reactjs
我正在使用 google 联系人 api 来获取联系人。
在获取方法的响应中,我获取了所有联系人。但是在使用 setState 设置状态时,它给出了 Cannot read property 'setState' of undefined
的错误。这是我的代码。
我也看过他的教程,但无法在其中找到确切的问题。
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
constructor(props) {
super(props)
this.auth = this.auth.bind(this);
this.state = {
userarray: []
}
}
auth() {
var config = {
'client_id': 'my-client-id',
'scope': 'https://www.google.com/m8/feeds'
};
window.gapi.auth.authorize(config, function () {
alert("Dd")
// this.fetchtt(window.gapi.auth.getToken());
fetch("https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + window.gapi.auth.getToken().access_token + "&max-results=700&v=3.0", {
method: 'GET'
})
.then((result) => {
result.json().then((result) => {
// display all your data in console
console.log(result.feed);
result.feed.entry.map((entry, index) => {
console.log(entry.title.$t)
const user = [
name => entry.title.$t
]
this.setState({
userarray: "ss"
});
})
})
}
)
});
}
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
<button onClick={() => this.auth()}>GET CONTACTS FEED</button>
</div>
);
}
}
export default App;
window.gapi.auth.authorize(config, function () {
将更改回调中的上下文,改用 window.gapi.auth.authorize(config, () => {
。
PS 你不需要嵌套你的 then -
.then(result => result.json())
.then(result => { ... })
我正在使用 google 联系人 api 来获取联系人。
在获取方法的响应中,我获取了所有联系人。但是在使用 setState 设置状态时,它给出了 Cannot read property 'setState' of undefined
的错误。这是我的代码。
我也看过他的教程,但无法在其中找到确切的问题。
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
constructor(props) {
super(props)
this.auth = this.auth.bind(this);
this.state = {
userarray: []
}
}
auth() {
var config = {
'client_id': 'my-client-id',
'scope': 'https://www.google.com/m8/feeds'
};
window.gapi.auth.authorize(config, function () {
alert("Dd")
// this.fetchtt(window.gapi.auth.getToken());
fetch("https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + window.gapi.auth.getToken().access_token + "&max-results=700&v=3.0", {
method: 'GET'
})
.then((result) => {
result.json().then((result) => {
// display all your data in console
console.log(result.feed);
result.feed.entry.map((entry, index) => {
console.log(entry.title.$t)
const user = [
name => entry.title.$t
]
this.setState({
userarray: "ss"
});
})
})
}
)
});
}
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
<button onClick={() => this.auth()}>GET CONTACTS FEED</button>
</div>
);
}
}
export default App;
window.gapi.auth.authorize(config, function () {
将更改回调中的上下文,改用 window.gapi.auth.authorize(config, () => {
。
PS 你不需要嵌套你的 then -
.then(result => result.json())
.then(result => { ... })