Node.js 我正在学习回调函数和API

Node.js 我正在学习回调函数和API,node.js,callback,Node.js,Callback,在这里,我学习了回调函数和API,以在节点上创建天气应用程序,但当我在终端上运行应用程序时,它会说未定义,我不知道为什么 const request = require("request"); request({ URL: "http://maps.googleapis.com/maps/api/geocode/json?address=1301%20lombard%20street%20philadelphia", json: true }, (error, response, body)

在这里,我学习了回调函数和API,以在节点上创建天气应用程序,但当我在终端上运行应用程序时,它会说未定义,我不知道为什么

const request = require("request");

request({
URL: "http://maps.googleapis.com/maps/api/geocode/json?address=1301%20lombard%20street%20philadelphia",
json: true
}, (error, response, body) => {
console.log(body);
});

您正在错误地调用
request
。你需要这样称呼它:

request("http://maps.googleapis.com/maps/api/geocode/json?address=1301%20lombard%20street%20philadelphia", {
    json: true
}, (error, response, body) => {
    console.log(body);
});
或者

request({
    url: "http://maps.googleapis.com/maps/api/geocode/json?address=1301%20lombard%20street%20philadelphia",
    json: true
}, (error, response, body) => {
    console.log(body);
});
注意
url
属性是小写的,而您的属性是大写的


请参阅

尝试记录
错误
响应
,如果您仍然需要帮助,请将这些信息发布在此处。还请发布一个指向您正在使用的
请求
库的链接,因为在不知道您正在使用的库的情况下,很难猜测回调的签名可能是什么。我尝试了body和response,结果在终端上未定义,当我尝试error时,它说operation.uri是必需参数。我今天已经安装了请求包,所以感觉很尴尬!