Introduction to Javascript - Problem at the very end of the course


#1

https://www.codecademy.com/courses/intermediate-javascript-requests/lessons/requests-ii/exercises/async-post-requests-iii?action=lesson_resume&course_redirect=introduction-to-javascript

I’m in point number 7:
“Add a body property that will contain the data to send to the Google URL Shortener API. The data object is {longUrl: urlToShorten} and this data should be passed to JSON.stringify() as an argument. Check the hint if you aren’t sure how to structure this.”

It doesn’t compile. No error apppears, even if I have used the mentioned code like in other episodes.


async function shortenUrl(){
  const urlToShorten = $inputField.val();
const urlWithKey = url + '?key=' + apiKey;
  
  try {
    let response = await fetch(urlWithKey,{
      method:'POST', 
      body: JSON.stringify({longUrl: urlToShorten})
  })
  
  catch(error){
    console.log(error)
  }
}

I suppose I am doing something wrong, but I can not see what it is.


#2

Is this the first query parameter in the list? If not, it should be preceded by an ampersand, not a question mark.


Belay the above. I’ve reached that lesson now and passed step 7.

async function shortenUrl() {
  const urlToShorten = $inputField.val();
  const urlWithKey = url + '?key=' + apiKey;
  try {
    let response = await fetch(urlWithKey, {
      method: 'POST',
      body: JSON.stringify({longUrl: urlToShorten})
    });    
  }
  catch(error) {
    console.log(error);
  }
}

As we can see, you are missing the closing curly brace before the catch() statement.


#3

It was the curly brace. Thank you!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.