首页 > 解决方案 > Owner_id 字段未通过验证错误。我的架构有什么问题?

问题描述

我正在调查我的架构有什么问题。我试图在我的集合中插入一个条目,但由于我已经改变了一些东西,但我遇到了很多错误,但这似乎是我最接近成功插入文档的地方。我在 React Ionic 项目中使用 mongodb-stitch-browser-sdk 并且我有一个有效的用户登录。

我使用 StitchUser.id 作为我的 owner_id 字符串(匹配用户集合中我的有效用户的 id)。

这是我的架构,后面是 Stitch 日志中的错误。我只是想在我的目标表中插入一个文档。此外,此集合上没有过滤器,并且只有一个具有以下规则的角色。

{
  "owner_id": "%%user.id"
}

这为用户提供了对他们创建的集合的读写权限。

{
  "bsonType": "object",
  "required": [
    "goalTitle",
    "startDate",
    "endDate",
    "owner_id"
  ],
  "properties": {
    "_id": {
      "bsonType": "objectId"
    },
    "owner_id": {
      "bsonType": "string",
      "validate": {
        "%or": [
          {
            "%%prevRoot.owner_id": {
              "%exists": false
            }
          },
          {
            "%%prevRoot.owner_id": "%%this"
          }
        ]
      }
    },
    "goalTitle": {
      "bsonType": "string",
      "minLength": {
        "$numberInt": "1"
      },
      "maxLength": {
        "$numberInt": "30"
      }
    },
    "goalDescription": {
      "bsonType": "string",
      "minLength": {
        "$numberInt": "0"
      },
      "maxLength": {
        "$numberInt": "600"
      }
    },
    "startDate": {
      "bsonType": "string"
    },
    "endDate": {
      "bsonType": "string"
    }
  }
}
Error:
role "owner" in "todo_list.Goals" does not have insert permission for document with _id: ObjectID("5e6aa8d11d233536e3ea8604"): could not validate document: 
    owner_id: Does not pass validation
Stack Trace:
StitchError: insert not permitted
Details:
{
  "serviceAction": "insertOne",
  "serviceName": "mongodb-atlas",
  "serviceType": "mongodb-atlas"
}
{
  "arguments": [
    {
      "collection": "Goals",
      "database": "todo_list",
      "document": {
        "goalTitle": "Test Goal",
        "goalDescription": "Test Description",
        "endDate": "2020-03-11",
        "startDate": "2020-03-10",
        "owner_id": "5e6891382e6039c1c32f7d46",
        "_id": {
          "$oid": "5e6aa8d11d233536e3ea8604"
        }
      }
    }
  ],
  "name": "insertOne",
  "service": "mongodb-atlas"
}

我创建了另一个没有架构的集合,并且对 owner_id 的相同规则检查和该集合中的文档都可以很好地插入。我不得不想象这是一个架构错误。

标签: mongodb-stitch

解决方案


推荐阅读