问题描述
我正在尝试创建一个firebase函数,该函数在创建新文档时都会发出HTTP POST请求.
I'm trying to create a firebase function that makes a HTTP POST request whenever a new document is created.
这是我的代码:
import * as functions from 'firebase-functions';
const admin = require('firebase-admin');
const request = require("request");
exports.sendMessage = functions.firestore.document('comms/{comms}').onCreate((snap, context) => {
const newValue = snap.data();
if (newValue) {
//const email = newValue.email;
const msg = newValue.msg;
return request({
uri: "url",
method: 'POST',
body: msg,
json: true,
resolveWithFullResponse: true
}).then((response: { statusCode: number; }) => {
if (response.statusCode >= 400) {
throw new Error(`HTTP Error: ${response.statusCode}`);
}
console.log('SUCCESS! Posted', msg);
});
}
return Promise
});
Error received:
TypeError:request(...).then不是一个函数在exports.sendMessage.functions.firestore.document.onCreate(/srv/lib/index.js:25:12)在cloudFunction(/srv/node_modules/firebase-functions/lib/cloud-functions.js:127:23)在/worker/worker.js:825:24在在process._tickDomainCallback(internal/process/next_tick.js:229:7)
TypeError: request(...).then is not a function at exports.sendMessage.functions.firestore.document.onCreate (/srv/lib/index.js:25:12) at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:127:23) at /worker/worker.js:825:24 at at process._tickDomainCallback (internal/process/next_tick.js:229:7)
推荐答案
request
本机支持回调接口但不返回promise ,这是您必须在内部执行的操作云功能.
request
supports callback interfaces natively but does not return a promise, which is what you must do within a Cloud Function.
在以下官方Firebase视频系列中对此进行了说明: https://firebase.google.com/docs/functions/video-series/.尤其要观看标题为学习JavaScript承诺"的三个视频(第2和第3部分特别关注后台触发的Cloud Functions,但之前确实值得观看第1部分).
This is explained in the official Firebase video series here: https://firebase.google.com/docs/functions/video-series/. In particular watch the three videos titled "Learn JavaScript Promises" (Parts 2 & 3 especially focus on background triggered Cloud Functions, but it really worth watching Part 1 before).
您可以使用 request-promise
( https://github.com/request/request-promise )和 rp()
方法返回符合Promises/A +的常规承诺".然后,您将如下修改代码:
You could use request-promise
(https://github.com/request/request-promise) and the rp()
method which "returns a regular Promises/A+ compliant promise". You would then adapt your code as follows:
import * as functions from 'firebase-functions';
const admin = require('firebase-admin');
const rp = require('request-promise');
exports.sendMessage = functions.firestore.document('comms/{comms}').onCreate((snap, context) => {
const newValue = snap.data();
if (newValue) {
const msg = newValue.msg;
var options = {
method: 'POST',
uri: '....',
body: msg,
json: true // Automatically stringifies the body to JSON
};
return rp(options)
.then(parsedBody => {
// POST succeeded...
console.log('SUCCESS! Posted', msg);
return null;
})
.catch(err => {
// POST failed...
console.log(err);
return null;
});
} else {
return null;
}
});
这篇关于request(...).那么在发出POST请求时不是函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!