这是我创建的用于从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');
 }


node.js - 尝试在Firestore中保存时,参数返回Undefined-LMLPHP

node.js - 尝试在Firestore中保存时,参数返回Undefined-LMLPHP

最佳答案

如果在调用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

关于node.js - 尝试在Firestore中保存时,参数返回Undefined,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50583349/

10-09 21:47