代码之家  ›  专栏  ›  技术社区  ›  Geoff_S

reactjs函数现在无法使用response,因为我已更改为arrow函数

  •  0
  • Geoff_S  · 技术社区  · 5 年前

    我在react.js应用程序中调用了一个非常简单的函数,它查询pouchDB上的表并获取最大的值。这在以前是有效的,但它不允许我使用 setState then 阻止它告诉我它不能 rows 未定义的,未定义的 response

    response.rows 再一次为我的大活动设置状态值?

    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);
        })
    }
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   CertainPerformance    5 年前

    如果 .query 返回标准承诺 .then 只需要一个论点,而不是两个 (error, result) => { 拒绝 catch 街区,不在 then

    使用

    .then((response) => {
      console.log('largest is ' + response.rows[0].value);