I am trying to set a category color with an app.
At the moment I'm using the category browser dialog to select a category, then I parse the JSON results to extract the ID. That seems to work as the alert dialog shows the category ID I wanted. But the app fails to set the category color.
However if I uncomment the
var myid = 58306;
line then the category color is set.
So why does that work but the JSON.parse to extract the id doesn't even though it seems to have the same id?
Thanks in advance for any light being shed on this.
<body>
<div class="container">
<button id="btn-test" class="jzdark">test</button>
<script>
$(document).ready(function() {
$('#btn-test').click(function() {
// Let the user select one category.
IMatch.categoryBrowser({
options: 'default,singlesel'
}).then(function(response) {
var y = JSON.parse(JSON.stringify(response, null, 2));
var myid = y.id;
alert(myid);
// var myid = 58306;
IMWS.post('v1/categories/update', {
id: myid,
colorcodesettings: "enabled",
textcolor: "#ffffff",
backgroundcolor: "#00aa00"
})
});
});
});
</script>
</div>
</body>
id must be a string. Did you try
var myid = '58306';
If I check your code, y.id is an array, not a single (scalar) value. If you use
var myid = y.id[0];
instead it should work.
(Assuming you only want to set the color for one category at a time).
id takes a string. Either one id '123' or multiple ids separated with , : '1,5,6'
If you have an array or ids, use either one id (thrinn's anwer above) or use y.id.join(',') to make a comma-separated string of ids from your array.
John,
an additional hint: Using JSON.stringify and JSON.parse in combination does not make sense to me: The IMatch endpoint returns a JSON object. JSON.stringify converts this object to a string. Then, JSON.parse converts the string back to a JSON object. The result is the JSON object you started with.
I would just use the response as returned by IMatch:
<body>
<div class="container">
<button id="btn-test" class="jzdark">test</button>
<script>
$(document).ready(function () {
$('#btn-test').click(function () {
// Let the user select one category.
IMatch.categoryBrowser({
options: 'default,singlesel'
}).then(function (response) {
var myid = response.id[0];
alert(myid);
// var myid = 58306;
IMWS.post('v1/categories/update', {
id: myid,
colorcodesettings: "enabled",
textcolor: "#ffffff",
backgroundcolor: "#00aa00"
})
});
});
});
</script>
</div>
</body>
I appreciate your responses guys, you each shed some new light on this project for me. :)
Thorsten your suggestion makes sense to me and your propose solution does exactly what I want to do.
Thanks again!