question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not exact `currentPath` at array pushing

See original GitHub issue

Hi, your lib is great. I’m trying to use it in my project, but I have a problem. According to changes I want to reproduce the object and I use the currentPath and newValue fields. When I push an item to an array, the currentPath not too exact.

Example

let data = {};

let reproduced = {};

var p = ObservableSlim.create(data, true, function(changes) {
	console.log(JSON.stringify(changes));
	changes.forEach(change => {
	  _.set(reproduced, change.currentPath, change.newValue);
	});
});

p.user = {
	roles: ["admin"]
};

p.user.roles.push("member");


setTimeout(function() {
	console.log("REPRODUCED OBJECT:", JSON.stringify(reproduced));
}, 2000);

Output

[{"type":"add","target":{"user":{"roles":["admin","member"]}},"property":"user","newValue":{"roles":["admin","member"]},"currentPath":"user","proxy":{"user":{"roles":["admin","member"]}}}]
[{"type":"add","target":["admin","member"],"property":"1","newValue":"member","currentPath":"user.roles","proxy":["admin","member"]}]
REPRODUCED OBJECT: {"user":{"roles":"member"}}

So when I use the roles.push the currentPath doesn’t contain the index, just the property. But when I update object, the currentPath contains the property field too. I think the correct currentPath should be user.roles.1. What do you think?

Reproduce jsFiddle: https://jsfiddle.net/icebob/rtvcn685/

Thanks, Icebob

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
icebobcommented, Aug 13, 2018

Hi, I tested, and it’s working properly! 👍 Maybe someone else would be useful, this is my small code which rebuilds the object by changes (uses lodash’s set & unset functions):

changes.forEach(c => {
	if (c.type == "add" || c.type == "update") {
		_.set(obj.__getTarget, c.currentPath, _.cloneDeep(c.newValue));
	} else if (c.type == "delete") {
		_.unset(obj.__getTarget, c.currentPath);
	}
});
0reactions
icebobcommented, Aug 11, 2018

Thanks @ElliotNB, I’m checking…

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript push not pushing correct values - Stack Overflow
Note: The array objects created by slice are shallow copied arrays. ... It's because you push reference to tempArray not a copy. try:...
Read more >
Array push not working properly!! - ServiceNow Community
Array push not working properly!! Go to solution. gokulraj. Giga Expert ... Can anyone help me to findout where the exact issue is??...
Read more >
Everything You Need to Know About the JavaScript Array ...
Using an array push is helpful when you need to know how many data points are contained in an array or for adding...
Read more >
array_shift - Manual - PHP
array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All...
Read more >
Array | Apple Developer Documentation
Overview. Arrays are one of the most commonly used data types in an app. You use arrays to organize your app's data. Specifically,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found