I need to write to state information at each iteration of the script. How to do it correctly?
getPageLink(i) {
if(i > 0){
console.log(this.state.res) // aaa
new Promise((resolve, reject) => {
request(this.props.catalogLinks[i], function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('.post__title_link').each(function (i, element) {
console.log(this.state.res) // undefined
});
resolve("result");
} else {
console.log('sss', this.state.res) // undefined
reject("result");
}
});
}).then(
result => {
this.getPageLink(--i)
},
error => {
this.getPageLink(--i)
}
);
}
}
Now in the console:
aaa
bundle.js:53444 Uncaught TypeError: Cannot read property 'res' of undefined
How to fix the error?