0

infom_col is a collection which contains a field rkey which itself contains an array like this: rkeys -> brands so brands is an array of ids. These ids are reference to other documents in the collection. I need to collect all such dependent ids and delete the documents one by one.

This is my code. Throws an error: TypeError: Cannot read property 'rkeys' of undefined

function collect_data(id)
{
  db.infom_col.find({"_id":id}, function(err,doc) {
        if(err) throw err;
        else 
        {
          console.log("else: " + doc._id);
          console.log("length: " + doc.rkeys.length);
          if(typeof(doc[0].rkeys) != 'undefined' && doc[0].rkeys != null) {
               for(key in doc[0].rkeys){
                   doc[0].rkeys[key].forEach(function(entry) {
                  //console.log("entry: " + entry); 
                     ids.push(entry);
                  //console.log(ids);
                   });
                }
                if(ids.length > 0) {
                    var id1 = ids.pop();
                    console.log(id1);
                    db.infom_col.remove({"_id":id1}, function(err,doc1) {
                        if(err) throw err;
                        else {
                          console.log("deleted: " + id1);
                          var id2 = ids.shift();
                          console.log("going to delete: " + id2);
                          collect_data(id2,null);
                        } 
                    });
                }
            }
        } 
    });
 }

Update1:

function collect_data(id,cb)
{
   db.infom_col.findOne({"_id":id}, function(err,doc) {
       if(err) throw err;
       else 
       {
          console.log("else: " + doc._id);
          if (doc.rkeys) {
               console.log("length: " + doc.rkeys.length);
               for(key in doc.rkeys){
                  doc.rkeys[key].forEach(function(entry) {
                      console.log("entry: " + entry);
                      ids.push(entry);
                  });
               }
               if(ids.length > 0) {
                  var id1 = ids.shift();
                  console.log("id1: " + id1);
                  db.infom_col.remove({"_id":id1}, function(err,doc1) {
                        if(err) throw err;
                        else collect_data(id1,null); 
                  });
               }
       }
   } 
 });
}
blackmamba
  • 1,952
  • 11
  • 34
  • 59

1 Answers1

0

The error you are having: TypeError: Cannot read property 'rkeys' of undefined means that doc[0] is undefined.

First I would change the find to findOne:

db.infom_col.findOne({"_id":id}, function(err,doc) {

Then I would get rid of doc[0], because it's confusing.

After that, determine if doc.rkeys is set:

if (doc.rkeys.length > 0)

or, with underscore:

if (!_.isEmpty(doc.rkeys))
tpae
  • 6,286
  • 2
  • 37
  • 64
  • I tried this: else { // console.log("check rkeys: " + id); if(doc.rkeys.length > 0) { for(key in doc[0].rkeys){ And changed find to findOne. But nothing happens. Doesn't exit and doesn't delete anything. – blackmamba Dec 30 '13 at 20:27
  • Make sure to replace doc[0] with doc consistently throughout the code. you are still using doc[0].rkeys in your loop – tpae Dec 30 '13 at 20:32
  • also, try to console.log(doc), see if it returns anything. if you can paste some more information, i can help you better. – tpae Dec 30 '13 at 20:32
  • I tried this: console.log("else: " + doc); if (doc.rkeys.length > 0) { console.log("length: " + doc.rkeys.length); Prints first console as else: [object Object] then hangs. I think it's not going into the if part. – blackmamba Dec 30 '13 at 20:39
  • get rid of the if part, i think it can work without it. – tpae Dec 30 '13 at 20:53
  • I think I got the logic wrong earlier. Because initailly it pushed 1->2->3. SO it removed 3 from db but it assigned 3 to id1. So recursion took place on 3 which is not in the db. Modified the code. So that array pops from the beginning. But it deletes 1 nad 3 but not 2 and 4. – blackmamba Dec 30 '13 at 21:16
  • Hm yeah, maybe recursion in this case is a bad idea. I am assuming its async code somehow pulls different results each time, causing issues. I would try looking for non-recursive solution, and trigger remove as a static function: http://mongoosejs.com/docs/api.html#model_Model.remove – tpae Dec 30 '13 at 21:21
  • How about if we I collect the ids first and then iteratively delete it? But when I collect those ids it keeps on adding the data again and again. Recursion doesn't stop. I don't know what to add as base case. – blackmamba Dec 30 '13 at 21:29