1

i am using below code...but if Status is Empty iss show below result in database...i want to remove if Status is Empty ..

1 Address {}, Status {} save in database.

function CheckStatus(oldJob, newJob) {
  var obj = {};
  if(newJob && newJob.Status) { 
   obj.Status= {}; 
    if (oldJob.Status.total !== newJob.Status.total) {
      obj.Status.total = newJob.Status.total;
    }
    if (oldJob.Status.charge_description && oldJob.Status.charge_description !== newJob.Status.charge_description) {
      obj.Status.charge_description = newJob.Status.charge_description;
    }
  }
}

Mongodb

 "Job" : {
        "_id" : ObjectId("5873b352e7621d08fccc5890"),
        "updated_by" : "",
        "details" : "{\"current_status\":\"Completed\",\"address\":{},\"Status\":{}}",
        "changetype" : "Update",
        "datetime" : ISODate("2017-01-09T15:59:14.202Z")
    },

please help how what enter in If Condition ( below code not working)

 if(obj.address = '{}')
  {
   console.log('Empty');

  }
Addi Khan
  • 173
  • 1
  • 13

1 Answers1

1

Setting an object's value to undefined essentially deletes them. You may use the undefined keyword easily as such:
newJob.address = undefined;

How i would check for an empty object and delete its content:

if(Object.keys(newJob.address).length === 0 && obj.constructor === Object){
    newJob.address = undefined;
}

Solution is better explained in this question's answers:
How do I test for an empty JavaScript object?

Community
  • 1
  • 1
Simen Fjellstad
  • 374
  • 1
  • 16
  • i was change my code.. if(obj.address = 'undefined') not working – Addi Khan Jan 09 '17 at 16:33
  • Did you read the question i referenced? I only answered the part of "deleting" a variable. Also, you should check for undefined without quotation marks. And you never make comparisons with a single equals sign. – Simen Fjellstad Jan 09 '17 at 16:35
  • if(obj.address == undefined) not working when i enter if(obj.address = undefined) ..this working only empty .. – Addi Khan Jan 09 '17 at 16:43
  • Thanks for answer ..its working :) Your Good Man :) – Addi Khan Jan 09 '17 at 16:48
  • i need one more answer..please see my old post.. http://stackoverflow.com/questions/41533714/how-to-add-new-array-in-body-history-nodejs – Addi Khan Jan 09 '17 at 16:51
  • I am sorry, i do not have experience with mongoose. Though i believe you have to change it in the schema itself. – Simen Fjellstad Jan 09 '17 at 16:52