{ data: { detections: [ [Array] ] } }
the code below gives the error above :
const readline = require('readline');
const axios = require('axios');
const promptMessage = "Enter an English sentence: ";
const translateSentence = async (sentence) => {
const encodedParams = new URLSearchParams();
encodedParams.set('q', sentence);
const options = {
method: 'POST',
url: 'https://google-translate1.p.rapidapi.com/language/translate/v2/detect',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Accept-Encoding': 'application/gzip',
'X-RapidAPI-Key': 'e3c2a65895msh2ae6ac2073633ebp1e942ajsn83c6d7659148',
'X-RapidAPI-Host': 'google-translate1.p.rapidapi.com'
},
data: encodedParams,
};
try {
const response = await axios.request(options);
console.log(response.data);
} catch (error) {
console.error(GOOGLE TRANSLATE RESPONDED WITH ERROR CODE ${error.response.status}. MESSAGE: ${error.response.data.message});
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(promptMessage, (sentence) => {
if (!sentence) {
console.log("NO INPUTS");
} else if (sentence.length > 160) {
console.log("THE GIVEN SENTENCE EXCEEDED THE CHARACTER LIMIT");
} else {
translateSentence(sentence);
}
rl.close();
});
ATTEMPT : tried googling the error message. RESULT : No relevant solution found.
ATTEMPT 2 : tried asking chatsonic for solution. RESULT :
The error you are encountering is likely due to the absence of the "URLSearchParams" class. This class is part of the "url" module in Node.js and is used to handle URL query parameters.
To resolve this issue, you need to import the "url" module and create an instance of the "URLSearchParams" class. Here's an example of how you can modify your code to include the necessary import statement:
const readline = require('readline');
const axios = require('axios');
const { URLSearchParams } = require('url');
// Rest of your code...
By including the { URLSearchParams } in the import statement, you will be able to use the URLSearchParams class in your code without any errors.
ATTEMPT 3 : tried the solution above. RESULT : The same error still persists.