Updating an attribute: error I don't know how to fix

Started by ubacher, June 25, 2017, 06:26:23 PM

Previous topic - Next topic

ubacher

I read an attribute and Im returns:
get attrib returns:{
  "result": [
    {
      "id": 750963,
      "data": [
        {
          "instanceId": 1,
          "PageSequenceNr": "HellO"
        }
      ]
    }
  ],
  "debug": {
    "runtime": "0ms"
  }
}


I then try to update the attribute and I pass the following to the server:
{"setid":1,"id":750963,"actions":"[{\"op\":\"update\",\"data\":{\"PageSequenceNr\":\"HellO\",\"instanceid\":1}}]"}

(If I specify add instead of update it works OK!

However I get the error message:

Set attrib returns:{
  "result": "failed",
  "resultMessage": "instanceid is missing or not an array. To delete all instances, specify an empty array.",
  "debug": {
    "runtime": "0ms"
  }
}


How can I fix this. The full script is attached.
( PS: I did start out with the attributes sample script - but it did not help me with this)

Mario

From the top of my mind: instanceid must be an array.

Wrong:

instanceid: 1

Correct:

instanceid: [1]
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

ubacher

I tried all kinds of things in terms of arrays but no luck.

Mario

-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

ubacher

JSON I am sending was shown in my first post. Here again:
{"setid":1,"id":750963,"actions":"[{\"op\":\"update\",\"data\":{\"PageSequenceNr\":\"HellO\",\"instanceid\":1}}]"}

thrinn

In your script you attached to the original post you seem to call the SetAttribute function with 'replace' for the AddOrUpdate parameter. Should this not be 'update'?
Thorsten
Win 10 / 64, IMatch 2018, IMA

Mario

Your code uses

action.data.instanceid = 1;

but instanceid must be an array. Hence the "not an array" error message.,
Change to

action.data.instanceid = [1];


to make this an array (with one element in this case).

Note: Take care when you work with instance ids. These record numbers are to be considered volatile. Don't hard-code them in your scripts - except for testing purposes.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

ubacher

Does not work:
This is sent to the server: {"setid":1,"id":750963,"actions":"[{\"op\":\"update\",\"data\":{\"PageSequenceNr\":\"Hello\",\"instanceid\":[1]}}]"}
myscript attributes read write.js:120 Set attrib returns:{
  "result": "failed",
  "resultMessage": "instanceid is missing or not an array. To update all instances, specify an empty array.",
  "debug": {
    "runtime": "0ms"
  }
}

Mario

I've made a check with the Attributes Samples App.

Under 2. Updating Data I selected a set and then changed the text for the instance 1 of the currently selected file. Works without problems.
Did you try that? Does it work?
The JSON record sent to the server looks like this:

actions:[{"op":"update","instanceid":["1"],"data":{"Note":"A nice baby photo"}}]

   "actions": [{
      "op": "update",
      "instanceid": [1],
      "data": {
         "Note": "This is the new content of Note"
      }
   }]
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

ubacher

#9
Thank you - this info allowed me to find my error.

I had
action.data.instanceid = [1];

instead of
action.instanceid = [1];

What caused the confusion was that when getting the attribute it returns:
get attrib returns:{
  "result": [
    {
      "id": 748569,
      "data": [
        {
          "instanceId": 1,
          "Description": "hello",
          "PageSequenceNr": "000"
        }
      ]
    }
  ],
  "debug": {
    "runtime": "0ms"
  }

Where instanceId is under data. (Although this instanceid is spelled with a big I in Id.)

Mario

1. The data returned can be for multiple files and contain multiple instance ids (if a file has multiple rows of data), hence the instance id is always part of data.

2. As explained in the documentation (Naming Conversions), parameter names sent to IMWS are always lower case. Data returned in JSON format uses the standard camelCase syntax.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

thrinn

Quote2. As explained in the documentation (Naming Conversions), parameter names sent to IMWS are always lower case.
If only I would remember all that is written...  :-[
I did have quite a look at ubacher's example coding, but I did not see this.

Am I correct in assuming that the lower case rule is only valid for "native" IMatch parameters, while e.g. attribute names have to be spelled exactly the way they have been created?
Thorsten
Win 10 / 64, IMatch 2018, IMA

Mario

In JavaScript, everything is case-sensitive.

Since the IMWS endpoints are language-agnostic (work with all languages) and should also work with command-line tools like cUrl or wget (which need to code everything into the URL) I decided t make the endpoint names and parameters all lower-case.

By common standards, JavaScript code and, by extension, JSON data storage, should use camelCase names, starting with a lower letter. So you get instanceId, folderName, etc.

IMatch is also case-sensitive so the Category "family" is different from "Family". Same for Attributes. If you access a category or Attribute or file by path/name, always write them as they show up in IMatch itself. In case you do it wrong, you just get an "not found" back so you'll know.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook