请求未返回写入数据库.只显示控制台 [英] Request not returning Write to database. Just shows the console

查看:37
本文介绍了请求未返回写入数据库.只显示控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将响应保存到数据库中.但它仅显示控制台,并且不会将写入操作返回到数据库.

I am trying to save my response into the database. but it shows only the console and does not return the write to the database.

这是我的代码....

exports.saveGroups = functions.firestore.document("Users/{user_id}").onWrite((change,context) => {



token_id1 = change.after.data().token_id;
token_email = change.after.data().email;
image = change.after.data().image;
name1 = change.after.data().name;
user_id = context.params.user_id;
console.log('token_id1:' + token_id1);
console.log('token_email:' + token_email);
console.log('Image:' + image);
console.log('name:' + name1);
console.log('user_id' + user_id);

var headers = {
   'Authorization': 'key = AAAATmJbwHE:APA91bGIEsq0aioIzAa_7g5JvLX1NPU3z1Gkt6vxB2J-I_9IvllwDJaxjmEp5DPw6ZoEBmXfwiYwICrMuE0kQrvxuHTGPc5YKr3i-JNru-o6AHnrAjq4r7iZWmzUuSmPwu8CbR2kXEIq',
   'project_id': '336657629297',
   'Content-Type': 'application/json'
}


var options = {
    url: 'https://android.googleapis.com/gcm/notification',
    method: 'POST',
    headers: headers,
    json: {'operation': 'create',
    'notification_key_name': token_email,
    'registration_ids': [token_id1]}
}



 const promise = request(options, function (error, response, body) {
 tokenName = body.notification_key;
 console.log('Key: ' + tokenName); //here it shows me the correct value
 return db.collection('Users').doc(user_id).set({name: name1,token_id: token_id1,notification_key: tokenName,image: image,email: token_email}).then(() => {        //never reach this line
 return console.log("Document successfully written!");   //never returns this console       
 }).catch(function(error) {
    return console.error("Error writing document: ", error);
   });


  })

  return promise;           //finishes with status "ok"

 });

我已经阅读了promises文档.但我找不到任何处理请求"功能的示例.

I've gone through the promises documentation. but I don't find any example to handle the "request" functions.

需要帮助.预先感谢.

推荐答案

request 本机支持回调接口,但返回承诺,这是您必须在内部执行的操作云功能.

request supports callback interfaces natively but does not return a promise, which is what you must do within a Cloud Function.

我强烈建议您观看Firebase团队的这些视频: https://www.youtube.com/watch?v=7IkUgCLr5oA&t=28s https://www.youtube.com/watch?v=652XeeKNHSk ,它解释了这个关键概念.

I strongly suggest that you watch these videos from the Firebase team: https://www.youtube.com/watch?v=7IkUgCLr5oA&t=28s and https://www.youtube.com/watch?v=652XeeKNHSk which explain this key concept.

您可以使用请求承诺( https://github.com/request/request-promise )和 rp(...)方法,该方法返回符合Promises/A +的常规承诺",然后执行以下操作:

You could use request-promise (https://github.com/request/request-promise) and rp(...) method which "returns a regular Promises/A+ compliant promise" and then do something like:

....
return rp(options)  // <- You should return this promise within the Function
    .then(function (body) {
        const tokenName = body.notification_key;
        console.log('Key: ' + tokenName);
        return db.collection('Users').doc(user_id).set({name: name1,token_id: token_id1,notification_key: tokenName,image: image,email: token_email});
    })
    .catch(function (err) {
        console.log(err);
    });

这篇关于请求未返回写入数据库.只显示控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆