我正在创建一个函数,该函数从服务器获取一组用户对象,这些对象即对象,一旦兑现了承诺,我将再次进行提取以访问每个用户的传输数据,这些用户也是对象。当我拥有所有数据时,我必须通过地图收集每个对象的数据,最终目标是创建一个表,在该表中出现与每个用户相对应的用户数据和传输数据。 问题是,当我尝试访问添加到对象的属性时,它们在表中未定义,而如果我查看控制台,则包含对象的数组将正确地包含所有属性。有人可以告诉我这是什么吗?
在这里,我保留进行提取的函数代码:
Traceback (most recent call last):
File "C:/Users/Tom/AppData/Local/Programs/Python/Python37-32/Scripts/automate.py", line 4, in <module>
driver = webdriver.Opera()
File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\opera\webdriver.py", line 83, in __init__
service_log_path=service_log_path)
File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\opera\webdriver.py", line 62, in __init__
keep_alive=keep_alive)
File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Opera binary
(Driver info: OperaDriver=2.40 (a50783a565882ef2022bea655e8560f37ecf8afe),platform=Windows NT 6.1.7601 SP1 x86_64)
在这里,我保留在屏幕上显示表格的功能:
function users() {
var arrayPromises;
fetch('http://.users.php').then(response => response.json()).then(function (users) {
for (let i = 0; i < users.length; i++) {
fetch('http://transport.php', {
method: 'post',
body: "idtransport=" + users[i].transport,
headers: {"Content-Type": "application/x-www-form-urlencoded"}
})
.then(function (promise) {
return promise.json()i
}).then(function (promiseTransport) {
arrayPromises += users.map(function () {
return users[i].idTransport = promisesTransport.id, users[i].nameTransport = promisesTransport.name, users[i].url = promisesTransport.url;
});
});
}
allTable(users);
console.log(users)
})
}
在这里,我留下了表格和console.log的图像,因此您可以看到在控制台中,数组中的每个对象确实具有添加的属性,但是当在屏幕上显示它们时它们会有效地出现未定义(我已经在代码中翻译了变量和属性的名称,但是在图像中它们仍然以原始语言显示)。
答案 0 :(得分:0)
Promise.all(array)返回一个Promise,当数组中的所有promise解析时,它就会解析。
当我们创建多个对“ transport.php”的提取请求时,我们将promise链推入数组promises
中。我们等待Promise.all(promises)
解决,然后调用`allTable(users)
console.log(users)
可能指向数据的实时引用,因此当您查看数据时,承诺已经解决,但是调用allTable(users)
时,承诺尚未解决。
function users() {
// var arrayPromises;
var promises = [];
fetch('http://.users.php').then(response => response.json()).then(function(users) {
for (let i = 0; i < users.length; i++) {
promises.push(fetch('http://transport.php', {
method: 'post',
body: "idtransport=" + users[i].transport,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(function(promise) {
return promise.json()
})
.then(function(promiseTransport) {
// arrayPromises += users.map(function() {
// return users[i].idTransport = promisesTransport.id, users[i].nameTransport = promisesTransport.name, users[i].url = promisesTransport.url;
// });
// this is equivalent to the above, except it does not have an unecesary map.
users[i].idTransport = promisesTransport.id;
users[i].nameTransport = promisesTransport.name;
users[i].url = promisesTransport.url;
})
);
}
Promise.all(promises).then(function() {
allTable(users);
console.log(users);
})
})
}