这是我在从firestore获取数据后生成通知密钥所创建的功能。为新用户成功生成通知密钥,但是未定义"当试图在firestore中设置它时。
我已在以下代码中注释了与该问题相关的问题。
index.js
exports.saveGroups = functions.firestore.document("Users/{user_id}").onWrite((change,context) => {
const token_id1 = change.after.data().token_id;
const token_email = change.after.data().email;
const image = change.after.data().image;
const name1 = change.after.data().name;
const key = change.after.data().notification_key;
const user_id = context.params.user_id;
console.log('token_id is: ' + token_id1);
console.log('token_email is:' + token_email);
console.log('user_id is:' + user_id);
if (key === undefined) {
console.log('key was not present, hence create');
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]}
}
request(options,function (error, response, body) {
tokenName = body.notification_key; //here i get the notification_key successfully
return console.log(tokenName);
});
var data = {
image: image,
email: token_email,
name: name1,
notification_key: tokenName, // this field becomes undefined while saving to firestore
token_id: token_id1,
extra: 'created'
};
console.log('image : ' + image);
console.log('email : ' + token_email);
console.log('name : ' + name1);
console.log('tokenName : ' + tokenName);
console.log('token_id : ' + token_id1);
var setDoc = db.collection('Users').doc(user_id).set(data);
}else{
console.log('key is present , do not create but add');
}
答案 0 :(得分:2)
如果在调用tokenName
之前声明变量request()
怎么办?
exports.saveGroups = functions.firestore.document("Users/{user_id}").onWrite((change,context) => {
const token_id1 = change.after.data().token_id;
const token_email = change.after.data().email;
const image = change.after.data().image;
const name1 = change.after.data().name;
const key = change.after.data().notification_key;
const user_id = context.params.user_id;
console.log('token_id is: ' + token_id1);
console.log('token_email is:' + token_email);
console.log('user_id is:' + user_id);
if (key === undefined) {
console.log('key was not present, hence create');
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]}
}
let tokenName = "";
return request(options,function (error, response, body) {
if (!error && response.statusCode == 200) {
tokenName = body.notification_key;
var data = {
image: image,
email: token_email,
name: name1,
notification_key: tokenName,
token_id: token_id1,
extra: 'created'
};
console.log('image : ' + image);
console.log('email : ' + token_email);
console.log('name : ' + name1);
console.log('tokenName : ' + tokenName);
console.log('token_id : ' + token_id1);
return db.collection('Users').doc(user_id).set(data);
} else {
return false;
}
}else{
console.log('key is present , do not create but add');
return false;
}
您遇到问题的原因是变量范围。见http://exploringjs.com/es6/ch_variables.html