在Firebase Node.js中获取未定义的值

时间:2019-01-18 09:04:54

标签: node.js firebase firebase-realtime-database google-cloud-functions

通过android代码推送新节点时,在firebase中获取未定义的值,但是当手动添加意味着直接在firebase数据库中时,我正在获取数据。无法找出问题所在。

exports.onUserCreated = functions.database.ref('/fcm/{pushId}')
    .onCreate((snapshot, context ) => {
      // Do something when a new user is created
      var email = snapshot.val().userid;
      var name = snapshot.val().username;
      var score = snapshot.val().desc;

      console.log(name);
      console.log(email);
      console.log(score);

      return sendWelcomeEmaill(email, name, score);
    });

function sendWelcomeEmaill(email, name, score){
    const mailOptions = {
        from :  `${APP_NAME} <noreply@firebase.com>`,
        to: email,
      };

    mailOptions.subject = `Score in Quiz ${APP_NAME}!`;
    mailOptions.html = `Hey ${name || ''}! Your score in ${APP_NAME} Quiz is ${score}.<br />
    <img src="https://lh3.googleusercontent.com/5gapf20jRXFQ7XchrHByzUDQfZUPccJ417emPwHJNlY72dcazXi_X3urzAGq3f2rXPjlNlZmxj0fuiy88tJ3exVQuA6XEt6Dm4kFlq1efWtWh3f1gMZ8humwKjds3uWX-a8kxODCNym7xfT9CwjwPMGy_LapGBbdwbxn2v0KdgCW12gHXOElqRmSKCOZlLRVVQ5FrFkwjm4rp9EtbJngPbOASMoAVVGMucfsMRqX2KHRpKnvgsvDw4v8I7EgyvlF_59eAsnjUclzZHbTR7PMSDes9RC883H6h2oWt6ZiJ5--cp1dijI0-zap5M30RvCQzSXKAoDX1CkuwhRuzkBSa4ffYa1uq9Z38IcGxPFzZcyKMSG1sc7XRQE1oxSedkx8knlX46194-nyqkwxxVwvas3emTgPpGH_AjaW1BAZPLJl4B8Sks3hQg8S9gF492dNppgFiZV6pGELZ2glbQDD8o5S-Lj9vtYUjQb1tu9892zxPEqkGwqaVZ2buFnNgPm3iZKi8jS7WpjIzvJoXsMJ8y9LoT7l8N5xStOTMa6MKsol3-lT89y3UHHflPFmdHV42K_HnGi806_iNvFlgo5Czg0ZYBXCicwJCryu6ND2qASEQCIbHOVBmzyqUK-BhTj4HzDuHsIu_j7WJITqowxEleI=w69-h52-no">
    <br/>We hope you will enjoy our service. <br/> `;

    return mailTransport.sendMail(mailOptions).then(() => {
      return console.log('New welcome email sent to:', email);
    });
}

等待,但同时获得两种方法的用户ID。如果我的问题不清楚,请在评论中问我。

案例1的屏幕截图[通过android代码添加数据时]

enter image description here

手动添加[有效]时的情况2屏幕截图

enter image description here

这是我的用于在Firebase中推送数据的android代码

mDatabase3 = FirebaseDatabase.getInstance().getReference().child("fcm");

mDatabaseUser1.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                // long i =  -1* new Date().getTime();

                //  String user_id = mAuth.getCurrentUser().getUid();

                String id = acct.getEmail();
                Uri personPhoto = acct.getPhotoUrl();

                final DatabaseReference newPost3 = mDatabase3.push();

                //  high.child(cleanEmail).setValue(finalValue);

                //high.child(node).setValue(finalValue);


                // newPost3.child(user_id).child("time").setValue(ServerValue.TIMESTAMP);
                // newPost3.child(user_id).child("uid").setValue(mCurrentUser.getUid());
                newPost3.child("userid").setValue(acct.getEmail());

                newPost3.child("desc").setValue(finalValue);
              //  newPost3.child("userimage").setValue(personPhoto.toString());
                newPost3.child("username").setValue(acct.getDisplayName()).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {

                        if (task.isSuccessful()) {



                        }


                    }
                });

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

1 个答案:

答案 0 :(得分:2)

每次在引用中调用setValue()updateChildren()都会导致将数据写入数据库。这意味着在您的代码中,将在以下语句之后为新用户创建新节点:

final DatabaseReference newPost3 = mDatabase3.push();

newPost3.child("userid").setValue(acct.getEmail());

因此,此时数据库仅包含新节点的userid。由于现在已创建节点,因此将触发您的Cloud Function,并且此时usernamescore尚未定义。

为防止这种情况发生,您需要在一次数据库调用中为新用户编写整个节点。一种简单的方法是将所有数据放入HashMap

Map<String, Object> values = new HashMap<String, Object>();

values.put("userid", acct.getEmail());
values.put("desc", finalValue);
values.put("username", acct.getDisplayName());

mDatabase3.push().setValue(values);