[英]TypeError: someobject.somefunction(…).then is not a function
I have created a utility function for getting the total size of the webtable using protractor and javascript. 我创建了一个实用程序函数,用于使用量角器和JavaScript获取Web表的总大小。
this.getTableSize = function(tableElement, rowSelector, columnSelector){
return {
row: tableElement.all(rowSelector).count(),
column : tableElement.all(columnSelector).count()
}
};
However on using the same function , i am geeting the error: 但是,在使用相同的功能时,我遇到了错误:
tableActions.getTableSize(table,by.css("tr"),by.css("th")).then(function(obj){
console.log(obj);
})
The error which i am getting is : 我得到的错误是:
TypeError: tableActions.getTableSize(...).then is not a function
The reason your code is failing is because you are using .then()
on a function that does not return a promise
. 代码失败的原因是因为您在不返回
promise
的函数上使用.then()
。
Here's an example of a working promise
: 这是一个可行
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);
})
You can use this in your code to return a resolve
or reject
, and then you will be able to use .then()
. 您可以在代码中使用它来返回
resolve
或reject
,然后就可以使用.then()
。
https://medium.freecodecamp.org/promises-in-javascript-explained-277b98850de
https://medium.freecodecamp.org/promises-in-javascript-explained-277b98850de
You need to correct your method to handle the promises correctly. 您需要更正方法以正确处理承诺。
I assume that tableElement.all(rowSelector).count() returns a promise else you will have to handle the callbacks; 我假设tableElement.all(rowSelector).count()返回一个Promise,否则您将不得不处理回调。
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 so use. Promise.all不会使用bluebird的承诺返回已解析数据的数组,因此请使用。
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); })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.