-1

My database hierarchy. I'm using Firebase Realtime-Database

I'm trying to send string(name) to cloud functions, make cloud function do a search in "AllCharacterNames" node by received string(name) from Unity, and then, return found result string back to Unity (even if string null or empty).

My Unity function:

private void CheckIfNameExists(string name)
{
    var function = functions.GetHttpsCallable("checkName");

    function.CallAsync().ContinueWithOnMainThread((response) =>
    {
        if (response.IsFaulted || response.IsCanceled)
        {
            Debug.LogError("Fault!");
        }
        else
        {
            string returnedName = response.Result.Data.ToString();
            if(returnedName == name)
            {
                //Name already exists in database
            }
            else if (string.IsNullOrEmpty(returnedName))
            {
                //Name doesn't exist in database
            }
        }
    });
}

My Cloud Function:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.checkName = functions.https.onCall(async (data) => {

    if (!data.exists()) {
        return null;
    }

    const name = data.val();

    var dbRef = admin.database().ref("AllCharacterNames");

    return dbRef.orderByValue().equalTo(name).once("value").then(function (snapshot)
    {
        return snap.val();
    });

});

Can someone kindly explain what i'm doing wrong? Thanks!

1 Answers1

1

Try this

   dbRef.orderByValue().equalTo(name).once("value").then(function (snapshot)
        {
        var exists = (snap.val() !== null);
        if(exists){
            snap.forEach(function(childSnapshot) {
                var key = childSnapshot.key;
                var childData = childSnapshot.val();
                return childData;
            })
        }
        return null;
    });

Or try onRequest

exports.checkName = functions.https.onRequest(async(req, res) => {
let name = req.body.name;
dbRef.orderByValue().equalTo(name).once("value").then(function (snapshot)
        {
        var exists = (snapshot.val() !== null);
        if(exists){
            await snapshot.forEach(function(childSnapshot) {
                var key = childSnapshot.key;
                var childData = childSnapshot.val();
                return res.status(200)
                    .type('application/json')
                    .json({ results: childData, success: true });
            })
        }
        return res.status(401)
            .type('application/json')
            .json({ message: "ok", success: false });
    });
})
  • Hi! Even tho i got an error and needed to add `.catch`, and now i get error `data.val is not a function`, i will still accept your answer cuz example was what i needed. Thanks! – Hajiyev Elbrus Sep 08 '20 at 12:11
  • After some modifications, i managed to make Unity send string and Cloud Functions to search that string (with your help ofc), Log in FirebaseConsole shows proper string value here `console.log("childData IS = " + childData);`. But, Unity never gets a response back. Any ideas why? – Hajiyev Elbrus Sep 08 '20 at 13:38
  • try async function (snapshot) – Robert Mihai Ionas Sep 08 '20 at 15:28
  • and await snap.forEach(function(childSnapshot) { – Robert Mihai Ionas Sep 08 '20 at 15:29
  • Hi, thanks for suggestion! Now this is what my code looks like: [link](https://i.imgur.com/ZCoOmOI.png) Strange thing is Unity receives bottom part of code, this - `var name = "-"; return name;`, when Firebase Logs shows this: [link](https://i.imgur.com/DXvZdY5.png) – Hajiyev Elbrus Sep 08 '20 at 17:45
  • you use const name = "data" and after you define same variable var name = '-' - const – Robert Mihai Ionas Sep 08 '20 at 18:33
  • All right, thanks for helping me out. Managed to make it work using @Gene Bo comment [here](https://stackoverflow.com/questions/45588740/asynchronous-https-firebase-functions) – Hajiyev Elbrus Sep 09 '20 at 12:48