【问题标题】:Error while accessing nested JSON object访问嵌套 JSON 对象时出错
【发布时间】:2015-10-28 06:06:53
【问题描述】:

这是我的 RethinkDB 表中的示例行。

 {
  "a1":  "val1" ,
  "a2":  "val2" ,
  "a3":  "val3" ,
  "a4":  "val4" ,
  "part": [
  {
  "id":  "reql" ,
  "position":  "student"
  } ,
 {
 "id":  "sdsadda" ,
 "position":  "officer"
 }
 ] ,
 "a5":  "val5" 

 }

我想访问一个嵌套的 json 对象,但我收到错误 e: Cannot perform bracket on a non-object non-sequence "string" 我需要输出中的整行匹配 id 到“reql”的行 这是我的查询

r.db('dbname').table('tablename').filter(r.row('part').contains(function(product) { return product('id').eq("reql"); }))

此查询以前有效。现在无效。

【问题讨论】:

    标签: rethinkdb


    【解决方案1】:

    如果您以某种方式在part 数组中得到一个字符串而不是对象的元素,您就会收到该错误。尝试运行.filter(r.row('part').contains(function(product) { return product.typeOf().ne('OBJECT'); }),它应该会返回part 数组中包含字符串的所有行。

    【讨论】:

    • 是否可以编写一个查询来给出结果,尽管 id 在某些行中是字符串而在所有其他行中是对象?
    • 您可以将 contains 的正文替换为 product.typeOf().eq('OBJECT').and(product('id').eq('reql')) 之类的内容。
    【解决方案2】:

    关于你的评论@Puja,我认为这应该为你做:

    r.db('dbname').table('tablename').filter(function(d){
        d("part").typeOf().eq("ARRAY");
    }).filter(r.row('part').contains(function(d) {
        return d('id').eq("reql");
    }))
    

    虽然,这比@mlucy 的答案效率低,您绝对应该只通过修复part: STRING 所在的所有文档来清理您的数据集。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      相关资源
      最近更新 更多