2

Path 1 - Match_Creator/cricket/matchList; Path 2 - Match_Creator/cricket/completedMatchList; I have a collection called matchList (Path 1) In which i am having a doc called c434108. Now I want to move this doc(c434108) to Path 2;

/* eslint-disable promise/catch-or-return */
const functions = require("firebase-functions");
const admin = require("firebase-admin");

const { db } = require("./db/index");

const createCompletedMatchListDoc = (request, response) => {
  completedMatchDocsData();
};

function completedMatchDocsData() {
  createNewCompletedMatchDocs()
 }

function getOldCompletedMatchDocs(){

  var completedMatchesRef = db
  .collection("Match_Creator")
  .doc("cricket")
  .collection("matchList");

  var completedMatchDocData;
  var completedMatchDataArr = [];

return new Promise(resolve => {
    let query = completedMatchesRef
      .where("status", "==", "live")
      .get()
      .then(snapshot => {
        // eslint-disable-next-line promise/always-return
        if (snapshot.empty) {
          console.log("No matching documents.");
          return;
        }

        snapshot.forEach(doc => {
          completedMatchDocData = doc.data();     
          completedMatchDataArr.push(completedMatchDocData);
          resolve(completedMatchDataArr);
        });
        console.log("sarang", completedMatchDataArr[2]);
      })
      .catch(err => {
        console.log("Error getting documents", err);
      });
    });
}

const createNewCompletedMatchDocs = (async(change, context) => {
  let completedMatchData = await getOldCompletedMatchDocs();

  console.log('aman', completedMatchData[1]);

  const newValue = change.after.data();
  const previousValue = change.before.data();

  const st1 =newValue.status;
  const st2 = previousValue.status;
  console.log('I am a log entry' + st1 + ' ' + st2);

  var data = completedMatchData[0];

  return db.collection('Match_Creator').doc('cricket').collection('completedMatchList').add(data)
  .catch(error => {
    console.log('Error writting document: ' + error);
    return false;
  });
})

module.exports = createCompletedMatchListDoc;

And After copy this doc(c434108) i want to delete this doc(c434108) from path 1.

And My index.js file is:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const storeMatchData = require("./liveScoring");
const createCompletedMatchListDoc = require("./completedMatchList");

var http = require("https");

module.exports = {
  liveScoring: functions.https.onRequest(storeMatchData),
  createCompletedMatchListDoc: functions.https.onRequest(
    createCompletedMatchListDoc
  )
};
Aman Singh
  • 195
  • 1
  • 3
  • 11

2 Answers2

1

I am able to solve my problem.

This is my completeMatchList.js file

/* eslint-disable promise/catch-or-return */
const functions = require("firebase-functions");
const admin = require("firebase-admin");

const { db } = require("./db/index");

const createCompletedMatchListDoc = (request, response) => {
  completedMatchDocsData();
};

function completedMatchDocsData() {
  setNewCompletedMatchDocs()
 }

function getOldCompletedMatchDocs(){

  var completedMatchesRef = db
  .collection("Match_Creator")
  .doc("cricket")
  .collection("matchList");

  var completedMatchDocData;
  var completedMatchDataArr = [];

return new Promise(resolve => {
    let query = completedMatchesRef
      .where("status", "==", "live")
      .get()
      .then(snapshot => {
        // eslint-disable-next-line promise/always-return
        if (snapshot.empty) {
          console.log("No matching documents.");
          return;
        }

        snapshot.forEach(doc => {
          // completedMatchDocData = doc.data();
          completedMatchDocData = {
            docId: "",
            docData: ""
          } 
          completedMatchDocData.docId = doc.id;
          completedMatchDocData.docData = doc.data();

          completedMatchDataArr.push(completedMatchDocData);
          resolve(completedMatchDataArr); // Here i am getting the data and pushing it in array
        });
        console.log("sarang", completedMatchDataArr);
      })
      .catch(err => {
        console.log("Error getting documents", err);
      });
    });
}

const setNewCompletedMatchDocs = (async () => {
  let getCompletedMatchData = await getOldCompletedMatchDocs();
  // console.log("balram", getCompletedMatchData[0].docId);
  let newCompletedMatchDocRef = db.collection("Match_Creator").doc("cricket").collection("completedMatchList").doc(getCompletedMatchData[0].docId);
  return newCompletedMatchDocRef.set(getCompletedMatchData[0].docData); //set/copy the data to new path.
})

This is my main index.js file

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const storeMatchData = require("./liveScoring");
const createCompletedMatchListDoc = require("./completedMatchList");

const { db } = require("./db/index");

var http = require("https");

module.exports = {
  liveScoring: functions.https.onRequest(storeMatchData),
  createCompletedMatchListDoc: functions.https.onRequest(
    createCompletedMatchListDoc
  )
};

Now after copy document data to a new path i will delete the previous document. For deleting the document i have not written the function.

Aman Singh
  • 195
  • 1
  • 3
  • 11
0

I'm not seeing anything that would allow you to move a document between collections(someone correct me if I'm wrong). You have to copy from the old collection to the new one and then remove the old one.

This is another post on StackOverflow that is running into this same issue and someone provided Java code on how to implement it.

EDIT: Updated link.

Hope this helps.

Stefan G.
  • 890
  • 5
  • 10
  • i know that i cannot move document between collections. I can only copy data. The link You have provided is based on angular but i want it in javascript or nodejs – Aman Singh Dec 12 '19 at 09:07
  • I pasted the wrong link, I updated the post with the link with Java I mentioned. – Stefan G. Dec 12 '19 at 09:27
  • i have updated my code in my question. when i am trying to add/copy data which i am getting from getOldCompletedMatchDocs() function to new path which i am trying to do in createNewCompletedMatchDocs() functon. I am getting an error – Aman Singh Dec 12 '19 at 09:41
  • Error which i am getting : UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'after' of undefined > at createNewCompletedMatchDocs (/Users/amansankrit/Desktop/predicta-function/functions/completedMatchList.js:54:27) > at process._tickCallback (internal/process/next_tick.js:68:7) > (node:3264) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise – Aman Singh Dec 12 '19 at 09:42
  • And i have edited my code also. You can check it above – Aman Singh Dec 12 '19 at 09:47
  • Please help me with the code for this problem.@Stefan G. – Aman Singh Dec 12 '19 at 10:41
  • Hey I think the issue resides in getOldCompletedMatchDocs, the function is not declared as async and you are returning a promise. Here you have an explanation of unhandlepromiserejectionwarning errors like the one you are getting. https://stackoverflow.com/questions/39716569/nodejs-unhandledpromiserejectionwarning – Stefan G. Dec 12 '19 at 16:40
  • not able to solve the error. Please help @Stefan G. – Aman Singh Dec 13 '19 at 08:00