我必须创建两个不同的变量(具有相同的名称(作为axios(req数据(的选项。
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,
};
}
公理:
const res = await axios.post(
'http://74c26025c4e2.ngrok.io/api/database/addbook',
item,
config,
);
显然,当我将变量放入 if/else 时,其余变量无法访问。我能做什么? 谢谢
如果两个测试都不成功怎么办?您的示例中没有纯else
语句,因此您确定undefined
允许item
吗?
如果是这样,正如 @marco-disco 所建议的那样,您可以在第一个if
语句上方定义一个变量:
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,
};
}
如果你真的想item
是一个常量,你可以使用三元运算符:
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
或使用 IIFE(立即调用的函数表达式(:
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
}
})()