Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用云函数和角度更新数据_Angular_Firebase_Google Cloud Functions - Fatal编程技术网

Angular 使用云函数和角度更新数据

Angular 使用云函数和角度更新数据,angular,firebase,google-cloud-functions,Angular,Firebase,Google Cloud Functions,我是firebase环境的新手。 我想使用云函数和角度服务更新firebase中的数据。 我能够使用云函数从firebase获取角度数据,如下所示 readUsers(): Observable<Person[]>{ return this._http .get(“https://us-central1-partner-f3f0b.cloudfunctions.net/getCustomer") .map(res => res.json()); } exports.getCu

我是firebase环境的新手。 我想使用云函数和角度服务更新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” });
});
我在这里面临的挑战是如何在angular 4中以json格式传递数据,并使用google cloud函数在firebase数据库中进行更新。
非常感谢您的帮助。

您只需将您的JSON放在请求的正文内容(应该是一篇帖子)中,然后通过
req.body.xxxxx


例如,如果您的请求正文内容是以下JSON{original:“New trainer”},您将通过执行
req.body.original

额外问题:您为什么不使用web库从web客户端与Firebase交互?谢谢你的回答。请你详细说明如何在正文中以角度传递请求。你能给出一个示例代码吗?@BikramSao恐怕不行。。。我不是专门研究棱角的。就我个人而言,我使用Vue.js,在这个特定情况下,我使用Axios()。您将在示例部分找到如何使用Axios发送POST请求。我在网上快速搜索了“带angular的POST请求”,发现了这样一个问题:我想您只需将数据声明为对象即可。