TypeError: someobject.somefunction(...).then is not a function

TypeError: someobject.somefunction(...).then is not a function

我创建了一个实用函数,用于使用量角器和 javascript 获取 webtable 的总大小。

this.getTableSize = function(tableElement, rowSelector, columnSelector){

        return {

            row: tableElement.all(rowSelector).count(),
            column : tableElement.all(columnSelector).count()
        }

    };

但是在使用相同的功能时,我遇到了错误:

tableActions.getTableSize(table,by.css("tr"),by.css("th")).then(function(obj){
          console.log(obj);
      })

我得到的错误是:

 TypeError: tableActions.getTableSize(...).then is not a function

您的代码失败的原因是因为您在 return 没有 promise.

的函数上使用 .then()

这是一个有效的例子 promise:

let promise1 = new Promise( (resolve, reject) => {
    let dataReceivedSuccessfully = false; 

    if (dataReceivedSuccessfully) {
        resolve('Data Available!'); 
    }
    if (!dataReceivedSuccessfully) {
        reject('Data Corrupted!'); 
    }
}) 

promise1.then( (success) => {
    console.log(success); 
}).catch( (err) => {
    console.log(err);
})

你可以在你的代码中使用这个来return一个resolvereject,然后你就可以使用.then().

https://medium.freecodecamp.org/promises-in-javascript-explained-277b98850de

您需要更正您的方法以正确处理承诺。

我假设 tableElement.all(rowSelector).count() returns 一个承诺,否则你将不得不处理回调;

this.getTableSize = function (tableElement, rowSelector, columnSelector) {
  return Promise.all([tableElement.all(rowSelector).count(), tableElement.all(columnSelector).count()]).then(function(data) {
    return {
      row: data[0],
      column: data[1]
    }
  })
};

tableActions.getTableSize(table, by.css("tr"), by.css("th")).then(function (obj) {
  console.log(obj);
})

Promise.all does not return the array of resolved data with bluebird promises这么用。

this.getTableSize = function (tableElement, rowSelector, columnSelector) {
  return ableElement.all(rowSelector).count().then(function(c) {
    return ableElement.all(columnSelector).count().then(function (c2) {
      return {
        row: c,
        column: c2
      } 
    })
  })
};

tableActions.getTableSize(table, by.css("tr"), by.css("th")).then(function (obj) {
  console.log(obj);
})