【发布时间】:2020-06-01 05:14:45
【问题描述】:
我想使用超出其范围的“标签”变量,我该怎么做?我知道这不是一个好习惯,但我仍然想使用标签数组中的值。我尝试克隆变量,但范围问题仍然存在
const client = new vision.ImageAnnotatorClient();
// Performs label detection on the image file
client
.labelDetection(`${filename}`)
.then(results => {
var labels = results[0].labelAnnotations;
console.log('Labels: ');
labels.forEach(label => console.log(label.description));
})
.catch(err => {
console.error('ERROR: ', err);
});
编辑 1: 我想这样使用标签
const client = new vision.ImageAnnotatorClient();
// Performs label detection on the image file
client
.labelDetection(`${filename}`)
.then(results => {
var labels = results[0].labelAnnotations;
console.log('Labels: ');
labels.forEach(label => console.log(label.description));
})
.catch(err => {
console.error('ERROR: ', err);
});
console.log('Labels: ');
labels.forEach(label => console.log(label.description));
但控制台显示错误“Cannot use undefined for forEach”
【问题讨论】:
-
您应该从
.then()中返回标签。使用 async / await,您可以访问承诺范围之外的值。
标签: javascript arrays scope javascript-scope