如何使用Promise设置变量的值,然后使用Express将其发布到db?

时间:2019-12-22 22:45:24

标签: javascript node.js express promise

我想使用node-zillow模块来设置lastSoldDate的值,但是当我发布时,我得到错误lastSoldDate是不确定的。这是我到目前为止的内容:

 router.route('/add').post((req, res) => {
        const address = req.body.address
        const street = address.street
        const zipcode = address.zipcode
        const city = address.city
        const state = address.state

        paramaters = {
        address: address.street,
        citystatezip: address.city + ", " + address.state
        }

        zillow.get('GetDeepSearchResults', paramaters)
      .then(function(results) {
        date = results.response.results.result[0].lastSoldDate[0]
        console.log(date)
        return date;
      }).then((date) => lastSoldDate = date)

        const lastSoldPrice = req.body.lastSoldPrice

        const newProperty =  new property({
            address:{
                street,
                zipcode,
                city,
                state,
            },
            lastSoldDate,
            lastSoldPrice
        })

        newProperty.save()
            .then(() => res.json('Property added!'))
            .catch(err => res.status(400).json('Error: ' + err))
    })

0 个答案:

没有答案