-2

我想使用超出其范围的“标签”变量,我该怎么做?我知道这不是一个好习惯,但我仍然想使用标签数组中的值。我尝试克隆变量,但范围问题仍然存在

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”

4

2 回答 2

0

.then()当您在async上下文中时,您只能使用 a 或在 Promise 之外访问该值:

(async () => {
  const client = new vision.ImageAnnotatorClient();
  const labels = client
    .labelDetection(`${filename}`)
    .then(results => results[0].labelAnnotations)
    .catch(err => console.error('ERROR: ', err))

  console.log('Labels: ');
  labels.forEach(label => console.log(label.description));
})()

这是一个工作示例,getLabels可以替代您当前的代码:

(async () => {
  const fakeData = [{
    labelAnnotations: [
      { description: 'label 1' },
      { description: 'label 2' },
      { description: 'label 3' },
    ]
  }]

  const getLabels = () => new Promise(r => setTimeout(r, 1000, fakeData))
  const results = await getLabels()
  const labels = results[0].labelAnnotations
  
  console.log('Labels: ');
  labels.forEach(label => console.log(label.description));
})()

于 2020-02-17T10:44:07.593 回答
-1

只需labels在承诺之外定义:

const client = new vision.ImageAnnotatorClient();
let labels = null;

// Performs label detection on the image file
client
    .labelDetection(`${filename}`)
    .then(results => {
        labels = results[0].labelAnnotations;

        console.log('Labels: ');
        labels.forEach(label => console.log(label.description));
    })

    .catch(err => {
        console.error('ERROR: ', err);
    });

不确定你想在哪里使用它,但你只能在 promise 解决后才能使用它的值。在这种情况下,更好的方法是:



I want to use the "labels" variable beyond its scope, how can I do it ? I know its not a good practice but I still want to use the values in labels array. I tried cloning the variable but the scope issue persists

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));
        return labels;
    })

    .then(labels => {
        console.error('Some other usage for labels: ', labels);
    });


    .catch(err => {
        console.error('ERROR: ', err);
    });
于 2020-02-17T09:50:57.673 回答