Search code examples
javascriptpodio

Update App Item Field using Podio-js PUT request


I am using the Podio-js SDK to GET data from an App Item, format it into a URL, and PUT that URL back into a field in the same item. The PUT request either returns an empty response (sometimes saying that it 'cannot read property 'values' of undefined, or it empties the existing text in the field instead of updating it. I think it has to do with my formatting of the information I pass it. I have tried formatting in JSON object format with {"key" : 'value'} pair syntax but I get similar results.

app.post('/signup',urlencodedParser, function(req, res) {

  //gather information from the item student registry entry and format a link
  //to pre-populated Podio webform

  podio.isAuthenticated().then(function() {

    var itemID = Object.keys(req.body)[0];
    var token = podio.authObject.accessToken;
    var itemPath = `/app/${creds.appID}/item/${itemID}?oauth_token=${token}`;
    var fieldPath = `/item/571453849/value/signup-link`;
    var link;   

    podio.request('GET', itemPath).then(function(responseData) {

        //this request works
        //console.log(JSON.stringify(responseData, null, 4));

          var student = {
            "studentID": responseData.app_item_id,
         }

         var requestData = { url: `www.example.com/${student.studentID}` }

         console.log('\n');
         console.log('fieldpath: ' + fieldPath + '\n');
         console.log('generatedlink: ' + link + '\n');

    }).catch(function(f) {
        console.log(f)
        })

    podio.request('PUT', fieldPath, link).then(function(responseData) {
        //I want to PUT this item 'link' back into a field in the same 
        //item, but I get the error message below
        console.log(JSON.stringify(responseData, null, 4));
        res.end();
    }).catch(function(f) {
        console.log(f)
        })
})

Solution

  • I was not handling promises correctly. I put the GET and PUT request in the same promise chain with the same .catch at the end. Also, I formatted my request as seen below and am successfully able to PUT data in fields.

    app.post('/signup', urlencodedParser, (req, res) => {
    
        podio.isAuthenticated()
        .then(function() {
    
            var appItemID = Object.keys(req.body)[0];
            var token = podio.authObject.accessToken;
            var itemPath = `/app/${creds.appID}/item/${appItemID}?oauth_token=${token}`;
    
            return podio.request('GET', itemPath)
        })
        .then(function(responseData) {
            //this request works
            //console.log(JSON.stringify(responseData, null, 4));
            var student = {
                "itemID": responseData.item_id,
            }
    
            var fieldPath = `/item/${student.itemID}/value/signup-link-2`;  
            var requestData = { url: `https://podio.com/webforms/[formattedLink`
    
            return podio.request('PUT', fieldPath, requestData)
        })
        .then(function(responseData) {
    
            res.end(JSON.stringify(responseData, null, 4))
        })
        .catch(function(f) {
            console.log(f)
            res.end(JSON.stringify(f, null, 4))
        })
    })