在Node.js / Express.js / Javascript / Ecmascript中的函数范围之外时,对象不会保存

时间:2019-03-12 22:17:42

标签: javascript node.js express ecmascript-6 handlebars.js

我正在使用Node.js和Express开发一个应用程序。我的一个路由文件中包含以下内容:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By


SITE_URL = 'https://www.facebook.com/pg/mnagaazdorp/posts/'
POSTS_XPATH = "//*[contains(@class, '_4-u2') and contains(@class, '_4-u8')]"
POST_TEXT_CLASS = "userContent"
TIMEOUT = 1
CHROME_DRIVER_PATH = "C:\\Users\\tonda\\Documents\\chromedriver.exe"

browser = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)

browser.get(SITE_URL)

wait = WebDriverWait(browser, TIMEOUT)

posts = browser.find_elements_by_xpath(POSTS_XPATH)

for post in posts:
    post_text_element = post.find_element_by_class_name(POST_TEXT_CLASS)
    print(post_text_element.text)

browser.quit()

router.get('/', (req, res) => { Manufacturer.find({}, (err, manufacturers) => { if(err) { console.log(err) req.flash('error', 'No manufacturers were found') res.redirect('/') } let allAircrafts = {"manufacturers": {}} for(let key in manufacturers) { let currentManufacturerAircrafts = [] let manufacturerName = manufacturers[key].name Aircraft.find({manufacturer_name: manufacturerName}, (err, aircrafts) => { if(err) { console.log(err) req.flash('error', 'No aircrafts were found') res.redirect('/') } for(let akey in aircrafts) { currentManufacturerAircrafts.push(aircrafts[akey]) } // let manufacturerObj = {name: manufacturerName, aircrafts: currentManufacturerAircrafts} allAircrafts["manufacturers"][manufacturerName] = currentManufacturerAircrafts // console.log(allAircrafts) #1 }) } // console.log(allAircrafts) #2 res.render('aircrafts', { expressFlash: req.flash(), sessionFlash: res.locals.sessionFlash, allAircrafts}) }) }) 对象应该存储所有制造商的所有飞机,当我在下面的#1中allAircrafts它确实起作用时,我得到了期望值。 当我在#2中console.log时,我只会得到这个:

console.log

我不知道为什么要向我显示此内容,但是我怀疑它没有正确保存它。

0 个答案:

没有答案