0

When this code executes, the result is

userId : { userName : userN }

when it actually should be the params from the rest call. How can I dynamically add data to ref.update?

exports.addUser = functions.https.onRequest((req, res) => {
  const userId = req.query.userId;
  const userN = req.query.userName;
  var ref = admin.database().ref("users")
  ref.update({
   userId : {
    userName : userN
  }
  });
  res.status(200).send("User added successfully");  
});

1 Answer 1

1

I guess you need to specify that userId is a variable.

 exports.addUser = functions.https.onRequest((req, res) => {
    const userId = req.query.userId;
    const userN = req.query.userName;

    const ref = admin.database().ref("users")

    ref.update({
      [userId]: {
        userName: userN,
      },
    });

    res.status(200).send("User added successfully");
  });

PS: I don't know firebase

Sign up to request clarification or add additional context in comments.

3 Comments

undefined: { userName: userN } Didn't help. Thanks for the help though.
So userId is undefined, console.log(req.query) to check if it's here
your first answer actually did the job. userId was undefined before

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.