Skip to content Skip to sidebar Skip to footer

Null Value When Calling A Recursive Function In Node.js

I have this scenario I'm trying to map one JSON definition to another, I have a recursive function named find which works perfectly when I do simple operations but when I send comp

Solution 1:

Try with:

var mapToModel = function(res){
  var processes = res.definitions.process;
  var diagrams = res.definitions.BPMNDiagram; 
  var documents = [];
  for(var prop in processes) {
    if(processes.hasOwnProperty(prop)){
      var propertyNames = Object.getOwnPropertyNames(processes[prop]);
      for(var property in processes[prop]){
        var mapping ={};
        if(property==="$"){
          //do something with the process
        }else{
        //shapes
          mapping.hash = hash.hashCode(new Date().toString());
          mapping.type = property;
          mapping.value = processes[prop][property];
          var bpmnItem = find(processes[prop][property], function(x) {return x.$.id;});
          var bpmnId = bpmnItem.$.id;//value is ok

            if(bpmnId!=undefined){
              var returnVal;
              find(diagrams,function(x){

                if( x.$ != undefined && x.$.bpmnElement != undefined ){

                  if(x.$.bpmnElement===bpmnId){
                    {returnVal =  x;}
                  }
                }
              });

              console.log("return:"+ returnVal);
          }
          documents.push(mapping);
        }
      }
    }
     return documents;
  }
}

Solution 2:

I don't exactly what you are going for, but undefined is not the same as null. In your code, you only return if f(elem) is truthy, so all other cases the result will be undefined (because you didn't return anything else). If if your code is failing with a more complex object, then it must not be finding anything.

EDIT: after looking at your example data, I don't see anywhere where an id fields matches bpmnElement, so your find function will never find anything.


Post a Comment for "Null Value When Calling A Recursive Function In Node.js"