【发布时间】:2018-06-26 05:41:56
【问题描述】:
我正在使用带有 typescript 的 google firebase 函数。我有一个关于更好的代码管理的基本问题。目前我的代码如下所示:
export const on_command_ping = functions.database.ref("commands/ping/{id}").onWrite(async (change, context) => {
if(instr == '/my-sr'){
const reoptions = {
uri: baseUrl + '/serviceRequests',
headers: {
'Authorization': "Basic " + btoa(username + ":" + password)
},
json:true
};
const result = await rp.get(reoptions)
.then(function(resp){
console.log("got the response dude:" + JSON.stringify(resp))
const options = {
uri: respUrl,
method: "POST",
json: true,
body: { "attachments": [{
"fallback": "Sorry failed to get response"}]
}
}
return rp(options);
}));
}else if(instr == '/my-oher-stuff'){
//another REST call
}
正如您在上面看到的那样,在一个函数中管理所有内容太难了。那么如何组织这段代码,以便每个其余调用都是基于 if-else 从上面调用的单独函数。
【问题讨论】:
-
在你的代码中
instr永远不会被定义。 -
对不起,我吃了一些与我的问题无关的片段,以保持简短。
标签: typescript firebase code-organization