RethinkDB: iterating over object properties

徘徊边缘 提交于 2019-12-12 08:34:46

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/21864052/rethinkdb-iterating-over-object-properties

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!