【发布时间】:2018-09-30 22:12:41
【问题描述】:
我是 firebase 环境的新手。 我想使用 Angular 服务使用云功能更新 Firebase 中的数据。 我可以使用云功能从 Firebase 获取角度数据,如下所示
readUsers(): Observable<Person[]>{
return this._http
.get(“https://us-central1-partner-f3f0b.cloudfunctions.net/getCustomer")
.map(res => res.json());
}
云功能如下
exports.getCustomer = functions.https.onRequest((req, res) => {
res.header(‘Content-Type’, ‘application/json’);
res.header(‘Access-Control-Allow-Origin’, ‘*’);
res.header(‘Access-Control-Allow-Headers’, ‘Content-Type’);
//respond to CORS preflight requests
if (req.method === ‘OPTIONS’) {
res.status(204).send(‘’);
}
// Get a database reference to our posts
var db = admin.database();
var ref = db.ref(“customer”);
// Attach an asynchronous callback to read the data at our posts reference
ref.on(“value”, function (snapshot) {
console.log(snapshot.val());
res.status(200).json({ customer: snapshot.val() });
}, function (errorObject) {
console.log(“The read failed: “ + errorObject.code);
});
});
以下云功能正在运行并更新 firebase 数据库。
exports.getOneProducts = functions.https.onRequest((req, res) => {
res.header(‘Content-Type’, ‘application/json’);
res.header(‘Access-Control-Allow-Origin’, ‘*’);
res.header(‘Access-Control-Allow-Headers’, ‘Content-Type’);
//respond to CORS preflight requests
if (req.method === ‘OPTIONS’) {
res.status(204).send(‘’);
}
// Get a database reference to our posts
var db = admin.database();
var ref = db.ref(“/messages/-L82AfhFTr4odsh1GMId”);
ref.update({ original: “New trainer” });
});
我在这里面临的挑战是如何以角度 4 传递 json 格式的数据并使用谷歌云功能在 firebase 数据库中更新。 任何帮助表示赞赏。
【问题讨论】:
标签: angular firebase google-cloud-functions