Ausgabe
Ich kämpfe mit dem Eigentumserfolg , der in der API (recaptcha) definiert ist .
/**
* The structure of response from the veirfy API is
* {
* "success": true|false,
* "challenge_ts": timestamp, // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
* "hostname": string, // the hostname of the site where the reCAPTCHA was solved
* "error-codes": [...] // optional
}
*/
Unten habe ich versucht, dieses Problem mit optionaler Verkettung zu beheben, aber es funktioniert nicht. Ich habe versucht, die Benutzeroberfläche auf verschiedenen Seiten zu verwenden, aber ich kann das damit nicht wieder beheben.
Unten Code:
import fetch from "node-fetch"
//...
try {
const response = await fetch(
`https://www.google.com/recaptcha/api/siteverify?secret=${process.env.GOOGLE_RECAPTCHA_SECRETKEY}&response=${captcha}`,
{
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
},
method: "POST",
}
);
const captchaValidation = await response.json();
if(captchaValidation?.success) // error: Property 'success' does not exist on type '{}'
} catch (error) {}
Wie kann ich Zugang zu dieser Eigenschaft erhalten?
Lösung
Es scheint, dass Shat .json nicht typbewusst ist und die Methode Promis<never< – .json zurückgibt
Ich kann eine Lösung vorschlagen, die Sie benötigen, um die Captcha-Typen zu löschen und Typoskript mitzuteilen, glauben Sie mir, dass es sich um diesen Typ handelt
export interface CaptchaResponse {
success: boolean;
challenge_ts: string;
hostname: string;
error-codes?: any[];
}
....
const rawResponse = await response.json();
// next line is nothing more just saying - believe me it is this type. It will not do type checking
const typedRespone = rawResponse as CaptchaResponse;
if(typedRespone.success)
....
Beantwortet von – Svetoslav Petkov
Antwort geprüft von – Dawn Plyler (FixError Volunteer)