我有一个非常简单的函数,我在react.js应用程序中调用它,该函数正在查询pouchDB上的表并获取最大值。这以前可以工作,但它不允许我使用 setState
所以我更改为箭头函数,现在在我的 then
block 中它告诉我它无法获取 rows
未定义,未定义指的是 response
我做错了什么,如何才能再次获得response.rows
并为我的最大Activities值设置State?
queryLargest = () => {
console.log('good');
function map(doc, emit) {
emit(doc._id, doc.activityLogged);
}
function reduce(keys, values, rereduce) {
return Math.max.apply(Math, values);
}
this.state.activitysDB.db.query({
map: map,
reduce: reduce
}).then((err, response) => {
console.log('largest is ' + response.rows[0].value);
this.setState({
largestActivity: response.rows[0].value,
});
console.log(this.state.largestActivity);
}).catch((err) =>{
console.log(err);
})
}
最佳答案
如果 .query
返回一个标准 Promise,其 .then
将仅采用一个参数,而不是两个 - (error, result) => {
语法是回调使用的,而不是 Promise。当 Promise 遇到错误时,它们会拒绝,然后在 catch
block 中处理,而不是在 then
block 中(通常)。
使用
.then((response) => {
console.log('largest is ' + response.rows[0].value);
关于javascript - 既然我更改为箭头函数,reactjs 函数就无法使用响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60908483/