我正在尝试使用 Cloud Functions 创建新帐户。 这是我的功能:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const cors = require('cors')({origin: true});
admin.initializeApp(functions.config().firebase);
// Create the server
const app = express();
app.use((err, req, res, _next) => {
console.log('Error handler', err);
console.log(req);
if(err){
res.status(400).send({
message:'error',
message2:req,
message3:req.body
});
} else {
admin.auth().createUser({
email: "user@example.com", //change with post request data
password: "secretPassword", //change with post request data
})
.then(function(userRecord) {
console.log("Successfully created new user:", userRecord.uid);
})
.catch(function(error) {
console.log("Error creating new user:", error);
});
}
});
app.use(cors);
app.get('/', (req, res) => {
res.send(`Hello ${req}`);
});
exports.registeration = functions.https.onRequest(app);
现在我只是想测试。我在客户端使用请求库:
request.post(
'urloffunction',
{ json: { key: 'Testing testing' } },
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
}
);
最佳答案
如果您想要使用 Express 应用来处理来自 Cloud Functions 的 HTTP 请求,则必须在该应用中声明至少一个与传入请求匹配的路由。您的示例未声明任何路由,这意味着您的任何代码都不会针对任何请求执行。
您可以在此处查看设置 Express 路线的官方示例:https://github.com/firebase/functions-samples/tree/master/authorized-https-endpoint/
关于javascript - 使用 Cloud Functions Request/Express 创建新帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49931963/