ReferenceError:未定义resolve [英] ReferenceError: resolve is not defined

查看:1568
本文介绍了ReferenceError:未定义resolve的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能来调用谷歌语音API。看起来一切都很好,但我找不到为什么它给我错误。我是节点和承诺的初学者所以不确定为什么会出现这个错误。

I have a function to call the google speech api. Looks all is good but I cannot find why its giving me the error. I am beginner with node and promises so not sure why this error appears.


ReferenceError:在index.js中未定义resolve:57

ReferenceError: resolve is not defined at index.js:57

问题在于这部分代码:

  return speech
    .longRunningRecognize(responses)
    .then(function(results) {
      var operation = responses[0];
      console.log("Operation: ", operation);
      return operation.promise();
    })
    .then(function(responses) {
      resolve(responses[0]);
      console.log("Result: ", JSON.stringify(responses[0]));
    })

承诺


operation.promise()(第57行)

operation.promise() (line 57)

不能得到解决。它希望解决
的承诺,但看起来它找不到解析函数。

cannot be resolved. It wants to resolve the promise, but it looks like it cannot find the resolve function.

google api的工作原理如下:

The google api works like this:


  • 首先,您进行api调用以上传数据并开始此过程。

  • 这将为您提供操作名称。

  • 此结果准备好后,应使用此名称获取结果(最多只需30秒)

我感觉它一切正常,呼叫已经完成,响应又回来了。代码等待,然后它想要解决,但它不能...

I have the feeling its all working, the call is made, the response comes back. The code waits and then it wants to resolve but it cannot...

我的代码是这样的(它的云功能)

My code is like this (its a cloud function)

  exports.transcribeAudio = functions.storage.object().onChange(event => {
  const object = event.data;
  const filePath = object.name;
  const fileName = filePath.split("/").pop();
  const fileBucket = object.bucket;
  const bucket = gcs.bucket(fileBucket);
  const tempFilePath = path.join(os.tmpdir(), fileName);

  // Exit if this is triggered on a file that is not an image.
  // Get the file name.
  //const fileName = path.basename(filePath);
  console.log(filePath + " name: " + fileName);
  // Exit if the image is already a thumbnail.
  if (!filePath.startsWith("ucl-flac-audio")) {
    console.log("Only flac-audio need to be converted");
    return true;
  }
  // Exit if this is a move or deletion event.
  if (object.resourceState === "not_exists") {
    console.log("This is a deletion event.");
    return true;
  }

  return Promise.resolve()
    .then(() => {
      const audioFilename = "gs://" + fileBucket + "/" + filePath;
      console.log(audioFilename);
      const request = {
        config: {
          encoding: "FLAC",
          languageCode: "fr-FR"
        },
        audio: {
          uri: audioFilename
        }
      };

      return speech
        .longRunningRecognize(request)
        .then(function(responses) {
          var operation = responses[0];
          console.log("Operation: ", operation);
          return operation.promise();
        })
        .then(function(responses) {
          resolve(responses[0]);
          console.log("Result: ", JSON.stringify(responses[0]));
        })
        .catch(function(err) {
          console.error("Failed to get transcript.", err);
          //    reject(err);
        });
    })
    .catch(err => {
      return Promise.reject(err);
    });
});


推荐答案

你不打电话给 .then()处理程序中解析()。那里没有定义这样的功能。如果要在 .then()处理程序中设置promise的已解析值,则只需返回该值。

You don't call resolve() from within a .then() handler. There is no such function defined there. If you want to set the resolved value of the promise from within a .then() handler, you can just return that value.

更改此:

.then(function(responses) {
      resolve(responses[0]);
      console.log("Result: ", JSON.stringify(responses[0]));
})

到此:

.then(function(responses) {
      console.log("Result: ", JSON.stringify(responses[0]));
      return responses[0];
})






FYI, resolve()你可能会想到是新Promise执行者回调的参数:


FYI, the resolve() you are likely thinking of is an argument to the new Promise executor callback:

let p = new Promise(function(resolve, reject) {
    resolve(10);
});

而且,这是您使用它的上下文。

And, this is the context in which you would use it.

这篇关于ReferenceError:未定义resolve的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆