Ошибка типа uncaught: невозможно прочитать свойство 'sayHello' из неопределенного в greetGenerically

1

Я пытаюсь вызвать API и вернуть строку, но я получаю сообщение об ошибке: Uncaught TypeError: Не удается прочитать свойство "sayHello" неопределенного в greetGenerically (hello.js: 62) в HTMLInputElement.btn.onclick(hello.js: 44 )

Я хочу знать, нужно ли мне менять возвращаемый тип моего класса или что может вызвать ошибку.

Основной файл JS:

  function init() {

    // You need to pass the root path when you load your API
    // otherwise calls to execute the API run into a problem

    // rootpath will evaulate to either of these, depending on where 
the app is running:
    // //localhost:8080/_ah/api
    // //your-app-id/_ah/api

    var rootpath = "//" + window.location.host + "/_ah/api";

    // Load the helloworldendpoints API
      // If loading completes successfully, call loadCallback function
    gapi.client.load('helloworldendpoints', 'v1', loadCallback, 
 rootpath);
 }

/*
 * When helloworldendpoints API has loaded, this callback is called.
 * 
  * We need to wait until the helloworldendpoints API has loaded to
  * enable the actions for the buttons in index.html,
 * because the buttons call functions in the helloworldendpoints API
 */
 function loadCallback () { 
    // Enable the button actions
    enableButtons ();
 }  

 function enableButtons () {
    // Set the onclick action for the first button
    btn = document.getElementById("input_greet_generically");
    btn.onclick= function(){greetGenerically();};

    // Update the button label now that the button is active
    btn.value="Click me for a generic greeting";

    // Set the onclick action for the second button
     btn = document.getElementById("input_greet_by_name");
    btn.onclick=function(){greetByName();};

    // Update the button label now that the button is active
     btn.value="Click me for a personal greeting";
 }

 /*
  * Execute a request to the sayHello() endpoints function
  */
 function greetGenerically () {
// Construct the request for the sayHello() function
var request = gapi.client.helloworldendpoints.sayHello();

// Execute the request.
// On success, pass the response to sayHelloCallback()
sayHelloCallback(request);

}

 /*
  * Execute a request to the sayHelloByName() endpoints function.
  * Illustrates calling an endpoints function that takes an argument.
 */
function greetByName () {
// Get the name from the name_field element
var name = document.getElementById("name_field").value;

// Call the sayHelloByName() function.
// It takes one argument "name"
// On success, pass the response to sayHelloCallback()
var request = gapi.client.helloworldendpoints.sayHelloByName({'name': name});
request.execute(sayHelloCallback);

}

 // Process the JSON response
// In this case, just show an alert dialog box
// displaying the value of the message field in the response
 function sayHelloCallback (response) {
alert(response.message);    

}

Мой конечный файл:

 package com.google.training.helloworld;

import com.google.api.server.:.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.api.server.spi.config.Named;

/**
 * Defines endpoint functions APIs.
 */
@Api(name = "helloworldendpoints", version = "v1",
   scopes = {Constants.EMAIL_SCOPE },
    clientIds = {Constants.WEB_CLIENT_ID, 
   Constants.API_EXPLORER_CLIENT_ID },
    description = "API for hello world endpoints.")

public class HelloWorldEndpoints {

    // Declare this method as a method available externally through 
 Endpoints
@ApiMethod(name = "sayHello", path = "sayHello",
        httpMethod = HttpMethod.GET)

public HelloClass sayHello() {
    return new HelloClass();
}

// Declare this method as a method available externally through Endpoints
@ApiMethod(name = "sayHelloByName", path = "sayHelloByName",
        httpMethod = HttpMethod.GET)

public HelloClass sayHelloByName (@Named("name") String name) {
    return new HelloClass(name);
}

}

Мой файл Hello Class:

 package com.google.training.helloworld;

public class HelloClass {
    public String message = "Hello World";

   public HelloClass () {

    }

    public HelloClass (String name) {
    this.message = "Hello " + name + "!";
    }

    public String getMessage() {
    return message;
    }
 }
Теги:

1 ответ

0

Хотелось прокомментировать, но для этого нужно всего лишь 50 представителей.

У вас есть <script src="https://apis.google.com/js/client.js?onload=init"></script> в вашем html файле?

  • 0
    Да. Это влияет на ошибку?
  • 0
    Просто проверка, как gapi кажется, определен в этом JS
Показать ещё 4 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню