【问题标题】:Uncaught Type Error: Cannot read property 'sayHello' of undefined at greetGenerically未捕获的类型错误:无法在 greetGenericly 处读取未定义的属性“sayHello”
【发布时间】:2018-04-06 10:04:10
【问题描述】:

我正在尝试调用 API 并返回一个字符串,但出现错误: 未捕获的类型错误:无法读取未定义的属性“sayHello” 在 greetGenericly (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;
    }
 }

【问题讨论】:

    标签: javascript java json api


    【解决方案1】:

    想发表评论,但只有不到 50 名代表才能发表评论。

    您的 html 文件中有 <script src="https://apis.google.com/js/client.js?onload=init"></script> 吗?

    【讨论】:

    • 只是检查 gapi 似乎是在那个 js 中定义的
    • 您在学习 CPD200 Hello Endpoints 课程吗?如果是,您是否将您的代码与解决方案进行了比较?
    • 是的,我正在使用谷歌应用引擎的教程应用程序,并且有一个解决方案文件夹,但我认为它没有所有文件可以自己运行。我只看到一个 .js 和两个类文件。我应该只是复制并粘贴过来吗? @jERCle
    • 我应该更改根路径吗?
    • 看这里:github.com/GoogleCloudPlatformTraining/… js 文件在 webapp 目录中,java 文件在 java/com/google/training/helloworld 目录中并比较文件。不幸的是,我无法在 Java 方面提供帮助
    猜你喜欢
    • 2019-07-31
    • 2021-12-22
    • 2015-01-06
    • 2017-07-26
    • 2019-02-26
    • 2018-12-03
    • 2017-06-05
    相关资源
    最近更新 更多