如何从 github url 的 module.exports.x lamba 无服务器主体中获取数据 json 原始数据?
How to have data json raw from a github url in the body of the module.exports.x lamba serverless?
我遇到这个问题(文件名jsonRead.js):
var getJSON = require("get-json");
'use strict';
module.exports.endpoint = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify(load()),
};
callback(null, response);
};
function load(){
getJSON('https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-province.json', function(error, response){
return response
});
}
当我从终端 sls offline
尝试这个和午餐时,我的输出是空的,但是如果我 console.log getJson 的响应,我可以从 [=37] 中看到 JSON 文件=].
谁能帮帮我?我不知道。
我的serverless.yml
org: name
app: my-express-application-app
service: my-express-application
provider:
name: aws
runtime: nodejs10.x
plugins:
- serverless-offline
functions:
loadJson:
handler: jsonRead.endpoint
events:
- http:
path: readjson
method: get
getJson
函数 return 是一个回调函数,因此您不能 return 从中调用。像这样调用处理程序回调:
module.exports.endpoint = (event, context, callback) => {
getJSON('https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-province.json', function(error, response){
if(error) {
throw new Error(error) // You can also use callback if preferred
} else {
callback(null, {
statusCode: 200,
body: response
})
}
}
}
我遇到这个问题(文件名jsonRead.js):
var getJSON = require("get-json");
'use strict';
module.exports.endpoint = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify(load()),
};
callback(null, response);
};
function load(){
getJSON('https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-province.json', function(error, response){
return response
});
}
当我从终端 sls offline
尝试这个和午餐时,我的输出是空的,但是如果我 console.log getJson 的响应,我可以从 [=37] 中看到 JSON 文件=].
谁能帮帮我?我不知道。
我的serverless.yml
org: name
app: my-express-application-app
service: my-express-application
provider:
name: aws
runtime: nodejs10.x
plugins:
- serverless-offline
functions:
loadJson:
handler: jsonRead.endpoint
events:
- http:
path: readjson
method: get
getJson
函数 return 是一个回调函数,因此您不能 return 从中调用。像这样调用处理程序回调:
module.exports.endpoint = (event, context, callback) => {
getJSON('https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-province.json', function(error, response){
if(error) {
throw new Error(error) // You can also use callback if preferred
} else {
callback(null, {
statusCode: 200,
body: response
})
}
}
}