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: resolve is not defined at 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)
cannot be resolved. It wants to resolve the promise, but it looks like it cannot find the resolve function.
The google api works like this:
- 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)
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);
});
});