LK__
LK__

Reputation: 6723

Array of pointers in Parse.com using REST API

I have a an object 'Post' which can have many 'Comments'. I want to add these comment objects (pointers) to an array in Post.

Currently I have added them in the following way using 'PUT':

{
  "comments": {
      "__op": "AddUnique",
      "objects": {
          "__type": "Pointer",
          "className": "Comment",
          "objectId": comment.commentId
      }
    }
  };

When I look in the object browser at the column defined as 'Array' in the 'Post' object I see it represented in the following way:

[
    {
        "$each": {
            "__type": "Pointer",
            "className": "Comment",
            "objectId": "FuH4dAgSfJ"
        }
    },
    {
        "$each": {
            "__type": "Pointer",
            "className": "Comment",
            "objectId": "ogxH4RjcJQ"
        }
    },
    {
        "$each": {
            "__type": "Pointer",
            "className": "Comment",
            "objectId": "bPECgp3o9D"
        }
    }
]

What am I doing wrong here?

Upvotes: 0

Views: 2104

Answers (3)

LK__
LK__

Reputation: 6723

The answer was that the syntax I used was wrong. Correct syntax should be (array syntax was missing within object):

{
  "comments": {
      "__op": "AddUnique",
      "objects": [
          {
              "__type": "Pointer",
              "className": "Comment",
              "objectId": comment.commentId
          }
      ]
    }
  };

Upvotes: 4

Robert Rowntree
Robert Rowntree

Reputation: 6289

hmmm "POST.comments" should be an array with just pointer entries and NOT with that "$each".

I use pointers and can copy a sample entry in the pointer col from the data browser containing similar pointer array .

[{"_type":"Pointer","className":"Pages","objectId":"SFHR2SsEby"},{"_type":"Pointer","className":"Pages","objectId":"X5YC0BROWT"},{"_type":"Pointer","className":"Pages","objectId":"4HuMAAt0nG"},{"_type":"Pointer","className":"Pages","objectId":"a5cesZeYzi"},{"_type":"Pointer","className":"Pages","objectId":"RZQ0vcgctS"},{"_type":"Pointer","className":"Pages","objectId":"hPbHjpl43p"},{"_type":"Pointer","className":"Pages","objectId":"FAPI5PEwmb"},{"_type":"Pointer","className":"Pages","objectId":"r5tYGhZIOE"}]

above sample from "Books" class where there is a "pages" property thats an array of pointers like you mean to have. In row 1, in the Pages column, next to the property name ,in grey there appears the label "Array" to declare the type of the column.

Maybe prior to when you started trying to use array of pointers in the "comments" field, you assigned some other data type to 'comments' that had the effect of assigning a diff native type to the column ( NOT an ARRAY ). Once typed , i dont think you can overload to array....

Just make a new field like "comments-2" and load the array of pointers there and see if you get a diff result.

OR a wild guess that your process is in some loop construct thats popping the extra "$each" property in front of the normal array element.

Upvotes: 0

Marius Waldal
Marius Waldal

Reputation: 9932

I think AddUnique is used for adding objects to an array stored in parse.

Try the same with AddRelation instead.

Upvotes: 0

Related Questions