Reputation: 2036
I'm trying to do destructuring in function parameters but I'm getting an error like below,
TypeError: Cannot destructure property
obj1
of 'undefined' or 'null'.
var deepDiffMapper = function() {
return {
VALUE_CREATED: 'created',
VALUE_UPDATED: 'updated',
VALUE_DELETED: 'deleted',
VALUE_UNCHANGED: 'unchanged',
map: function({
obj1,
obj2
}) {
if (this.isFunction(obj1) || this.isFunction(obj2)) {
throw 'Invalid argument. Function given, object expected.';
}
if (this.isValue(obj1) || this.isValue(obj2)) {
return {
type: this.compareValues(obj1, obj2),
data: (obj1 === undefined) ? obj2 : obj1
};
}
/* Error On Uncomment
var diff = {};
for (var key in obj1) {
if (this.isFunction(obj1[key])) {
continue;
}
var value2 = undefined;
if ('undefined' != typeof (obj2[key])) {
value2 = obj2[key];
}
diff[key] = this.map(obj1[key], value2);
}
for (var key in obj2) {
if (this.isFunction(obj2[key]) || ('undefined' != typeof (diff[key]))) {
continue;
}
diff[key] = this.map(undefined, obj2[key]);
}
return diff;
*/
},
compareValues: function(value1, value2) {
if (value1 === value2) {
return this.VALUE_UNCHANGED;
}
if (this.isDate(value1) && this.isDate(value2) && value1.getTime() === value2.getTime()) {
return this.VALUE_UNCHANGED;
}
if ('undefined' == typeof(value1)) {
return this.VALUE_CREATED;
}
if ('undefined' == typeof(value2)) {
return this.VALUE_DELETED;
}
return this.VALUE_UPDATED;
},
isFunction: function(obj) {
return {}.toString.apply(obj) === '[object Function]';
},
isArray: function(obj) {
return {}.toString.apply(obj) === '[object Array]';
},
isDate: function(obj) {
return {}.toString.apply(obj) === '[object Date]';
},
isObject: function(obj) {
return {}.toString.apply(obj) === '[object Object]';
},
isValue: function(obj) {
return !this.isObject(obj) && !this.isArray(obj);
}
}
}();
var result = deepDiffMapper.map({
"obj1": {
a: 'i am unchanged',
b: 'i am deleted',
e: {
a: 1,
b: false,
c: null
},
f: [1, {
a: 'same',
b: [{
a: 'same'
}, {
d: 'delete'
}]
}],
g: new Date('2017.11.25')
},
"obj2": {
a: 'i am unchanged',
c: 'i am created',
e: {
a: '1',
b: '',
d: 'created'
},
f: [{
a: 'same',
b: [{
a: 'same'
}, {
c: 'create'
}]
}, 1],
g: new Date('2017.11.25')
}
});
console.log(result);
Note : The above code is working fine if I pass as two arguments.
I'm getting error if I uncomment the commented line on the above code. I also tested destructuring in function with below code, Which works fine. I am not sure why is happening.
var sayHello = function ({ name, surname }) {
console.log(`Hello ${name} ${surname}! How are you?`);
};
sayHello({ name: 'John', surname: 'Smith' })
// -> Hello John Smith! How are you?
Upvotes: 2
Views: 2565
Reputation: 6909
You are calling recursively your map
function with wrong parameters.
diff[key] = this.map(obj1[key], value2)
This expects an object with obj1
and obj2
so it can destructure it properly.
You should try on both inner map
calls:
diff[key] = this.map({obj1: obj1[key], obj2: value2)
Upvotes: 2
Reputation: 13792
Function .map()
waits 1 object argument with 2 properties obj1
and obj2
. In commented out code, it is called with 2 arguments without these properties: map(obj1[key], value2);
. According to its signature, the call should be map({obj1: obj1[key], obj2: value2});
.
Upvotes: 1