Help

Re: Having trouble with output.set

Solved
Jump to Solution
3321 1
cancel
Showing results for 
Search instead for 
Did you mean: 
prolific
4 - Data Explorer
4 - Data Explorer

Hey Guys, Im sure there is a very simple answer here but i am very knew to javascript in general, i appreciate any insight anyone can give me. I am using the airtable script function to post new users to an external application via it's rest api. I am successfully able to create the new user but i wish to record the ID number that is returned in the call back, i would like to use this in later steps in the air table automation but cannot get output.set to work. My script looks like so 

let inputconfig = input.config();
console.log(inputconfig);
let name = inputconfig.name
let password = inputconfig.password
let username = inputconfig.username


const options = {
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json',
authorization: 'Bearer samsara_api_lxxxxxxxxxxxxxx'
},
body: JSON.stringify({name: name, username: username, password: password})
};

let response = await fetch('https://api.samsara.com/fleet/drivers', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));



How do i properly add output.set for the ID returned in response? 
Appreciate any insight HUGE! Thankyou

1 Solution

Accepted Solutions
kuovonne
18 - Pluto
18 - Pluto

Your issue is that in your chain of "then" statements, your last "then" doesn't actually return a value. It just logs the response to the console instead of saving the response to the variable. Remove the line .then(response => console.log(response))

Then your response variable will have the data object. Put console.log(response) on a new line after the end of your code and you will see it.
Then you can use output.set('id', response.data.id)

See Solution in Thread

2 Replies 2
kuovonne
18 - Pluto
18 - Pluto

Your issue is that in your chain of "then" statements, your last "then" doesn't actually return a value. It just logs the response to the console instead of saving the response to the variable. Remove the line .then(response => console.log(response))

Then your response variable will have the data object. Put console.log(response) on a new line after the end of your code and you will see it.
Then you can use output.set('id', response.data.id)

@kuovonne Outstanding!!! I was struggling for a quite a while, your solution works perfectly, thankyou so much!!!!