RethinkDB:​​迭代对象属性 [英] RethinkDB: iterating over object properties

查看:53
本文介绍了RethinkDB:​​迭代对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据结构:

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

节点

{
    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);
    })});
})

它应该是这样的:

{
    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?

推荐答案

map 迭代数组或流,而不是对象.您可以使用 keys() 获取密钥,然后对其进行迭代.

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

查询内容如下:

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

这篇关于RethinkDB:​​迭代对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆