/    Sign up×
Community /Pin to ProfileBookmark

Uncaught type error?

Hi,

I’m trying to follow and make this [translator](https://youtu.be/-Bk4siyhvDE) and am stuck at 19:38. When I set the target language to a language and type “hello” I get the following error in the console:

>

<br />

>
>

<b>Warning</b>: Undefined array key “lang_two” in <b>/Users/——–/Downloads/LangTranslator/process.php</b> on line <b>8</b><br />

>
>

<br />

>
>

<b>Fatal error</b>: Uncaught TypeError: StichozaGoogleTranslateGoogleTranslate::__construct(): Argument #1 ($target) must be of type string, null given, called in /Users/———/Downloads/LangTranslator/process.php on line 12 and defined in /Users/————/Downloads/LangTranslator/vendor/stichoza/google-translate-php/src/GoogleTranslate.php:108

>
>

Stack trace:

>
>

#0 /Users/———/Downloads/LangTranslator/process.php(12): StichozaGoogleTranslateGoogleTranslate-&gt;__construct(NULL)

>
>

#1 {main}

>
>

thrown in <b>/Users/——–/Downloads/LangTranslator/vendor/stichoza/google-translate-php/src/GoogleTranslate.php</b> on line <b>108</b><br />

The console is supposed to print “en”, for English as the auto-detect (source) language.

This is my process.php code so far:

[code]
<-?php

require_once ‘vendor/autoload.php’;

use StichozaGoogleTranslateGoogleTranslate;

$lang_one = $_POST[‘lang_one’];
$lang_two = $_POST[‘lang_two’];
$text = $_POST[‘text’];

if ($lang_one == “AUTO_DETECT”) {
$tr = new GoogleTranslate($lang_two);

$text = $tr->translate($text);

echo $tr->getLastDetectedSource(); // Output: en
} else {
echo $tr->setSource($lang_one)->setTarget($lang_two)->translate($text);
}`

[/code]

And app.js:

[code]
$(document).ready(function(){
$(“#convert”).on(“click”, function() {
var lang_one = $(“#lang_one”).val();
var lang_two = $(“#lang_two”).val();
var text = $(“#text”).val();

$.ajax({
url: “process.php”,
method: “post”,
data: {lang_one: lang_one, lang_two:lang_two, text: text},
success: function(status){
console.log(status);
},
});
});
});
[/code]

I’m not sure why the error shows up. It says a type error but I can’t really identify one in my code. I tried to add a try-catch block after seeing a SO post:

[code]<-?php

require_once ‘vendor/autoload.php’;

use StichozaGoogleTranslateGoogleTranslate;

$lang_one = $_POST[‘lang_one’];
$lang_two = $_POST[‘lang_two’];
$text = $_POST[‘text’];

try {
if ($lang_one == “AUTO_DETECT”) {
$tr = new GoogleTranslate($lang_two);

$text = $tr->translate($text);

echo $tr->getLastDetectedSource(); // Output: en
} else {
echo $tr->setSource($lang_one)->setTarget($lang_two)->translate($text);
}
}
catch (TypeError $e) {
echo ‘error!!’;
}
[/code]

And now it throws in the console:

>
>

<br />
> <b>Warning</b>: Undefined array key “lang_two” in <b>/Users/——–/Downloads/LangTranslator/process.php</b> on line <b>8</b><br />
> error!!

I downloaded composer (version 2.4.2), just like in the video, and followed the steps to import the google translate Github library. Can someone please help me fix this TypeError? Thanks!

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@tracknutSep 21.2022 — Your warning is pointing at the reference to $_POST['lang_two'] at the top of process.php being non-existent (ie, looking it up as an array key in $_POST[] fails). Where are you getting that variable from? Some html form is not shown here, I presume. Check that, maybe a typo in the form field name?
Copy linkTweet thisAlerts:
@NogDogSep 21.2022 — Yeah, since $_POST['lang_two'] was not defined, $lang_two got set to null, which is its own type -- i.e. it is not of type string, which apparently the GoogleTranslate constructor has as the required type for its constructor's parameter.

Therefore, it's almost always a good idea to do some form input validation before you try to actually use the input fields. ;)
×

Success!

Help @code44 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ,
analytics: Fullres
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Marika,
tipped: article
amount: 1000 SATS,

tipper: @hatem774,
tipped: article
amount: 1 SATS,

tipper: @nearjob,
tipped: article
amount: 1000 SATS,
)...