我有一个函数来调用google语音API。看起来一切都很好,但我无法找到为什么它给了我错误。我是节点和承诺的初学者,所以不知道为什么会出现此错误。ReferenceError:解析未定义
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() (line 57)
不能得到解决。它想要解决 的承诺,但它看起来像无法找到解析函数。
在谷歌API的工作原理是这样的:
- 首先,你做一个API调用来上传数据并开始处理。
- 这给你一个操作名称。
- 这个名字应该被随后用来得到结果,当结果准备好(只需要最多30秒)
我有一种感觉它的所有工作,在调用时,响应回来。代码等待,然后它要解决,但它不能...
我的代码是这样的(它的云功能)
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回调。只需'返回响应[0]',然后由.then创建的承诺将解析为该值。 –
btw,这个:'.catch(err => {Promise.reject(err); });'完全没有意义。只要删除这些行。 – Thomas