Javascript 参考错误:未定义解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46649949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ReferenceError: resolve is not defined
提问by Koen
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.
我有一个函数来调用谷歌语音 api。看起来一切都很好,但我找不到为什么它给我错误。我是 node 和 promise 的初学者,所以不确定为什么会出现这个错误。
ReferenceError: resolve is not defined at index.js:57
ReferenceError:resolve 未在 index.js:57 中定义
The problem is in this part of the code:
问题出在这部分代码中:
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]));
})
Where the promise
承诺在哪里
operation.promise() (line 57)
operation.promise()(第 57 行)
cannot be resolved. It wants to resolve the promise, but it looks like it cannot find the resolve function.
无法解决。它想解析promise,但是看起来找不到resolve 函数。
The google api works like this:
谷歌 api 的工作原理如下:
- First you do an api call to upload your data and start the process.
- This gives you back an operation name.
- This name should be used subsequently to get the result when the result is ready (only takes max 30 sec)
- 首先,您进行 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);
});
});
回答by jfriend00
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.
您不会resolve()从.then()处理程序内部调用。那里没有定义这样的函数。如果您想从.then()处理程序中设置承诺的已解析值,您可以只返回该值。
Change this:
改变这个:
.then(function(responses) {
resolve(responses[0]);
console.log("Result: ", JSON.stringify(responses[0]));
})
to this:
对此:
.then(function(responses) {
console.log("Result: ", JSON.stringify(responses[0]));
return responses[0];
})
FYI, the resolve()you are likely thinking of is an argument to the new Promise executor callback:
仅供参考,resolve()您可能想到的是新 Promise 执行器回调的参数:
let p = new Promise(function(resolve, reject) {
resolve(10);
});
And, this is the context in which you would use it.
而且,这是您将使用它的上下文。

