【问题标题】:Request not returning Write to database. Just shows the console请求不返回写入数据库。只显示控制台
【发布时间】:2018-06-02 11:44:40
【问题描述】:

我正在尝试将我的回复保存到数据库中。但它只显示控制台,不会将写入返回到数据库。

这是我的代码....

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"

 });

我已经浏览了 Promise 文档。但我没有找到任何处理“请求”函数的示例。

需要帮助。提前致谢。

【问题讨论】:

    标签: node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    request 原生支持回调接口,但返回承诺,这是您必须在 Cloud Function 中执行的操作。

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

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

    ....
    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);
        });
    

    【讨论】:

    • 我看到昨天 Frank Van Puffelen 回答了您的一个问题 (stackoverflow.com/questions/50644574/…),并且还建议,正如我在回答中所做的那样,您观看这些视频,解释 Cloud Functions 如何基于承诺.让我们坚持 :-) -> 在为 Cloud Functions 编写任何代码之前,必须观看这些视频!它将为您节省数小时的调试时间!
    • @riturpana 您有时间尝试上面提出的解决方案吗?
    猜你喜欢
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 2018-06-16
    相关资源
    最近更新 更多