Reputation: 565
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
Upvotes: 0
Views: 55
Reputation: 1971
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
}
})()
Upvotes: 1