0

I have to create two different variables (with the same name) as option for axios (the req data).

  var indentifiers = entry.volumeInfo.industryIdentifiers;

  console.log(indentifiers.length);


  if (indentifiers.length === 1) {
    const item = {
      isbn_13: 'Not Provided',
      isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
      kind: entry.kind,
      title: entry.volumeInfo.title,
      authors: entry.volumeInfo.authors,
      publishedDate: entry.volumeInfo.publishedDate,
      language: entry.volumeInfo.language,
      image1: entry.volumeInfo.imageLinks.thumbnail,
    };
  } else if (indentifiers.length === 2) {
    const item = {
      isbn_13: entry.volumeInfo.industryIdentifiers[1].identifier,
      isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
      kind: entry.kind,
      title: entry.volumeInfo.title,
      authors: entry.volumeInfo.authors,
      publishedDate: entry.volumeInfo.publishedDate,
      language: entry.volumeInfo.language,
      image1: entry.volumeInfo.imageLinks.thumbnail,
    };
  }

axios:

 const res = await axios.post(
        'http://74c26025c4e2.ngrok.io/api/database/addbook',
        item,
        config,
      );

Obviously when I put the variable into if/else is not reachable by the rest. What can I do? thanks

4
  • Define it using let above the if statement, and mutate it accordingly Commented Jun 11, 2020 at 12:33
  • I tried but probably with the wrong syntax Commented Jun 11, 2020 at 12:37
  • Don't give up . Commented Jun 11, 2020 at 12:40
  • thanksa. lot for the help Commented Jun 11, 2020 at 12:45

1 Answer 1

1

What if neither test succeed? You don't have a pure else statement in your example, so are you sure that undefined is allowed for item?

If so, as @marco-disco suggested, you can define a variable above your first if statement:

let item
if (indentifiers.length === 1) {
    item = {
      isbn_13: 'Not Provided',
      isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
      kind: entry.kind,
      title: entry.volumeInfo.title,
      authors: entry.volumeInfo.authors,
      publishedDate: entry.volumeInfo.publishedDate,
      language: entry.volumeInfo.language,
      image1: entry.volumeInfo.imageLinks.thumbnail,
    };
  } else if (indentifiers.length === 2) {
    item = {
      isbn_13: entry.volumeInfo.industryIdentifiers[1].identifier,
      isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
      kind: entry.kind,
      title: entry.volumeInfo.title,
      authors: entry.volumeInfo.authors,
      publishedDate: entry.volumeInfo.publishedDate,
      language: entry.volumeInfo.language,
      image1: entry.volumeInfo.imageLinks.thumbnail,
    };
  }

If you really want item to be a const, you can use a ternary operator:

const item = (indentifiers.length === 1) ? {
    const item = {
      isbn_13: 'Not Provided',
      isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
      kind: entry.kind,
      title: entry.volumeInfo.title,
      authors: entry.volumeInfo.authors,
      publishedDate: entry.volumeInfo.publishedDate,
      language: entry.volumeInfo.language,
      image1: entry.volumeInfo.imageLinks.thumbnail,
    };
  } : (indentifiers.length === 2) ? {
    const item = {
      isbn_13: entry.volumeInfo.industryIdentifiers[1].identifier,
      isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
      kind: entry.kind,
      title: entry.volumeInfo.title,
      authors: entry.volumeInfo.authors,
      publishedDate: entry.volumeInfo.publishedDate,
      language: entry.volumeInfo.language,
      image1: entry.volumeInfo.imageLinks.thumbnail,
    };
  } : undefined

or use an IIFE (immediately invoked function expression):

const item = (() => {
  if (indentifiers.length === 1) {
    return {
      isbn_13: 'Not Provided',
      isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
      kind: entry.kind,
      title: entry.volumeInfo.title,
      authors: entry.volumeInfo.authors,
      publishedDate: entry.volumeInfo.publishedDate,
      language: entry.volumeInfo.language,
      image1: entry.volumeInfo.imageLinks.thumbnail,
    };
  } else if (indentifiers.length === 2) {
    return {
      isbn_13: entry.volumeInfo.industryIdentifiers[1].identifier,
      isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
      kind: entry.kind,
      title: entry.volumeInfo.title,
      authors: entry.volumeInfo.authors,
      publishedDate: entry.volumeInfo.publishedDate,
      language: entry.volumeInfo.language,
      image1: entry.volumeInfo.imageLinks.thumbnail,
    };
  } else {
    return undefined
  }
})()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.