La Faulx
La Faulx

Reputation: 472

RethinkDB: iterating over object properties

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?

Upvotes: 8

Views: 2280

Answers (1)

neumino
neumino

Reputation: 4353

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")
  })
})

Upvotes: 10

Related Questions