I have a function to call the google speech API. Looks all is good but I cannot find why it is giving me the error. I am a 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:
I have the feeling it's 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);
});
});
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. let p = new Promise(function(resolve, reject) { resolve(10); });
The ReferenceError object represents an error when a variable that doesn't exist (or hasn't yet been initialized) in the current scope is referenced. ReferenceError is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage() .
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.
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:
let p = new Promise(function(resolve, reject) {
resolve(10);
});
And, this is the context in which you would use it.
Before using resolve
you need to pass the resolve
as argument:
In this case,
return new Promise((resolve, reject) => {
axios.post('YOUR URL HERE', params)
.then(response => {
resolve() // Here you can use resolve as it passed as argument
})
});
Note: You can use GET, POST for the axios call
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With