-1

router.post('/', function(req, res, next) {
      console.log('req.body from root', req.body)
      var tuneName = req.body.name // what is entered by user
      var tune = encodeURIComponent(tuneName)
      console.log('tune', tune)
      var tuneUrl = 'https://thesession.org/tunes/search?q=' + tune + '&format=json'
      console.log('tuneUrl', tuneUrl)
    
      // Get first unirest call to session.org with tune entered by user
        unirest.get(tuneUrl)
        .end(function (response) {
          console.log('response from unirest', response.body.tunes[0])
    
          _id = response.body.tunes[0]
          console.log('_id', _id)
  
            // first render
            res.render('tunes', { tunesSess: response.body.tunes[0]})
            console.log('id', response.body.tunes.id)
    
          // 2nd unirest call to session.org to get key
            var tuneUrlKey = 'https://thesession.org/tunes/' + tuneOnly.id + '?format=json'
            unirest.get(tuneUrlKey)
            .end(function (response) {
              console.log('key', response.body.settings[0].key)
              res.render('tunes', {tuneSessKey: response.body.settings[0], response.body.settings[0].key})
            })
          })
})
  })


})
})

不确定如何从第二个 url 呈现第二个 unirest 调用。我主要想从第二个 unirest api 调用中呈现信息,尽管如果我可以将两个调用都呈现到同一个页面,那就太好了。

4

1 回答 1

0

I figured out the answer to my question. My problem was in that I needed to have my res.render within the .end function and only have one of the res.render functions, where I had two. I also moved my res.render function outside of my tunesDB.insert function.

I still would love to know how to pass a variable between routes if that is possible, but this at least gave me working data from my unirest api call.

于 2015-07-26T23:50:59.700 回答