firebase函数检查子子名是否存在?

时间:2017-12-09 19:06:22

标签: javascript firebase firebase-realtime-database google-cloud-functions

您好我正在尝试编写一个函数,找到userid以下的所有用户名并确定用户名是否已存在

我的json看起来像这样

    {
  "ali": {
    "AccessToken": "asdadas",
    "CreatedAt": "2017/12/09",
    "DeletedAt": -1,
    "Email": "sadadada",
    "FirstName": "asdad",
    "LastName": "asdadad",
    "LoginType": 0,
    "Phone": "",
    "ThumbNailUrl": "",
    "UpdatedAt": -1,
    "UserName": "asdada"
  },
  "asdadad": {
    "AccessToken": "asdadas",
    "CreatedAt": "2017/12/09",
    "DeletedAt": -1,
    "Email": "asdadadada",
    "FirstName": "asdada",
    "LastName": "asdada",
    "LoginType": 0,
    "Phone": "",
    "ThumbNailUrl": "",
    "UpdatedAt": -1,
    "UserName": "asdadadada"
  }
}

我的功能看起来像这样

    exports.CheckUserName = functions.https.onRequest((req,res) => {
    const userName = req.query.userName;
    if (userName == "" || userName == null) {
        return res.json({
            result: "UserName can not be null or empty"
        })
    } else {
        // what i have to do in here :(
    }
})

我尝试了很多方法,但我不能这样做

1 个答案:

答案 0 :(得分:0)

我是java开发人员,所以我会告诉你逻辑。你可以在你的javascript中实现

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("userId");

rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  void onDataChange(DataSnapshot snapshot) {
    if (snapshot.hasChild("your username")) {
      // run some code
    }
  }
});

现在,您可以在username下找到所有id。 如果存在根据您的需要打印的东西

如果你不懂id 您可以将所有用户名保存在名为usernames的单独节点中 并将所有usernames放在那里,并始终检查该节点中是否存在usernames。非常简单:)