-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})
            })
          })
})
  })


})
})

Not sure how to render the 2nd unirest call from the second url. I mostly want to render info from the 2nd unirest api call although it would be great if I can render both calls to the same page.

1 Answers1

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.