1

I am trying to add new data fields into existing list in firebase realtime database. I am using push() to generate unique ID in one of my function. In the next function how do I map the new data under the same unique ID so that new data gets appended under the same ID. Below is the fulfillment code:

function reportHandler(agent){
 const t2 = agent.parameters.name;
 const t3 = agent.parameters.college;
 const t4 = agent.parameters.branch;
 const t5 = agent.parameters.year;

 return admin.database().ref('data').push().set({
  name: t2,
  college: t3,
  branch: t4,
  year: t5
});

This code works perfectly and creates a unique ID field in the database. What I want to do next is to add new data into the previously created unique ID field.

function detailsHandler(agent){
 const t1 = agent.parameters.prtype;
 const t6 = agent.parameters.desc;
 const t7 = agent.parameters.when;

 return admin.database().ref('data').push().set({
  prtype: t1,
  desc: t6,
  when: t7
});

This is another function which has some values. I want values from both the functions into one field with unique ID.

I am calling the functions with this

let intentMap = new Map();
intentMap.set('Report', reportHandler);
intentMap.set('Report.details', detailsHandler);
agent.handleRequest(intentMap);

New field with unique ID gets created but with values from only the first function
enter image description here

This is what I want(values from both the functions into one field)
enter image description here

akashm
  • 11
  • 2
  • To be clear (since this is tagged dialogflow) - these two updates will be done in different Intent Handlers? So you don't get the values for "name" and "desc" from the same Intent, correct? (If this assumption isn't correct - showing the Intents in question, and exactly how you have the handlers registered, or how these functions are being called, will help us help you.) – Prisoner May 27 '20 at 11:53
  • 1
    Yes, they are two different intent handlers. And the values for "name" and "desc" are from different intents. – akashm May 27 '20 at 12:18

1 Answers1

0

You can break your question down into two parts:

  • How do we update a record given its ID?
  • How does the second Intent Handler know what ID to update?

How do we update a record given its ID?

You can't use the push() method, since this will save the data at a new ID. You also don't want to use the set() method, since this replaces data at the ID with new data.

Instead, you want to get a reference to the record with the ID, and then update it with the update() method.

If we assume our ID is in a variable named, cleverly enough, id, this might look something like this

const ref = admin.database().ref('data').child(id);
return ref.update({
  prtype: t1,
  desc: t6,
  when: t7
});

But where do we get the ID from?

How do we pass the ID from the first Intent Handler to the second?

The ID is generated when we call push() in our first Intent Handler, so we need to capture it at that point.

But to get it to the second Intent Handler, we'll need to save it in a context parameter using the agent.context.set() method. The set() method takes the name of a context, an optional lifespan (how many Intent calls will this be saved for), and an object containing attribute/value strings.

This might look something like this:

const dataRef = admin.database().ref('data');
const newRef = dataRef.push();
const id = newRef.key;
return newRef.set({
  name: t2,
  college: t3,
  branch: t4,
  year: t5
})
.then( () => {
  agent.context.set({
    name: "reportInfo",
    lifespan: 5,
    parameters: {
      id: id
    }
  });
});

later, in your second Intent Handler, you'd get this ID out of the context using, you guessed it, agent.context.get(), and then getting the id parameter from it. So possibly something like this:

const reportInfoContext = agent.context.get('reportInfo');
const id = reportInfoContext.parameters.id;

and then use this as outlined above.

Prisoner
  • 49,922
  • 7
  • 53
  • 105