【发布时间】: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