RethinkDB: iterating over object properties
Asked Answered
C

1

8

I have the following data structure:

wall

{
    slug: "wall-slug",
    nodes: {
        "node1": "id-from-nodes-table-1",
        "node2": "id-from-nodes-table-2"
    }
}

nodes

{
    id: "id-from-nodes-table-1",
    something: "something"
}

Trying to merge document from nodes table into definite node in nodes object in wall table in this way:

r.db("test").table("walls").getAll("wall-slug", {index: "slug"}).map(function(row) {
    return row.merge({nodes: row("nodes").map(function(node) {
        return r.db("test").table("nodes").get(node);
    })});
})

And it supposed to look like this:

{
    slug: "wall-slug",
    nodes: {
        "node1": {object from nodes table got by value from this property},
        "node2": {object from nodes table got by value from this property}
    }
}

But I get "Cannot convert OBJECT to SEQUENCE" message - couldn't find a way to iterate over nodes object properties and replace it's property values with objects from another table - is there any?

Confidence answered 18/2, 2014 at 19:59 Comment(0)
S
10

map iterates on an array or stream, not an object. You can use keys() to get the keys, then iterate on them.

Here's what the query looks like:

r.db("test").table("walls").getAll("wall-slug", {index: "slug"}).map(function(row) {
  return row.merge({nodes: 
    row("nodes").keys().map(function(key) {
      return r.expr([key, r.db("test").table("nodes").get(row("nodes")(key))])
    }).coerceTo("object")
  })
})
Slade answered 18/2, 2014 at 20:19 Comment(1)
Sorry, I forgot some square bracket. I just edited the query, could you try again?Slade

© 2022 - 2024 — McMap. All rights reserved.