StrongLoop验证错误和必填字段

时间:2015-11-21 06:12:24

标签: mysql json api loopbackjs strongloop

我正在使用StrongLoop和Loopback来创建API。我已将内置的User模型扩展为我自己的名为people的模型。我不希望User模型在默认情况下要求提供类似的电子邮件,因此我关注了this work around,现在当我使用默认的User模型创建用户时,电子邮件就是'需要。但是当我使用我的people模型时,仍然需要它。我在这里缺少什么?

此外,当我将people模型连接到MySQL数据库时出现奇怪错误。如果我有默认的内存连接器,即使用db数据源,我可以创建用户就好了,但当我将它连接到MySQL数据库时,我得到:

{
  "error": {
    "name": "ValidationError",
    "status": 422,
    "message": "The `people` instance is not valid. Details: `username` is     invalid (value: \"username123\"); `email` is invalid (value: \"testing@email.com\").",
"statusCode": 422,
"details": {
  "context": "people",
  "codes": {
    "username": [
      "uniqueness.Error: ER_BAD_FIELD_ERROR: Unknown column 'realm' in 'field list'"
    ],
    "email": [
      "uniqueness.Error: ER_BAD_FIELD_ERROR: Unknown column 'realm' in 'field list'"
    ]
  },
  "messages": {
    "username": [
      "is invalid"
    ],
    "email": [
      "is invalid"
    ]
  }
},
"stack": "ValidationError: The `people` instance is not valid. Details: `username` is invalid (value: \"username123\"); `email` is invalid (value: \"testing@email.com\").\n    at /var/www/photo-app/node_modules/loopback-datasource-juggler/lib/dao.js:265:12\n    at ModelConstructor.<anonymous> (/var/www/photo-app/node_modules/loopback-datasource-juggler/lib/validations.js:487:13)\n    at ModelConstructor.next (/var/www/photo-app/node_modules/loopback-datasource-juggler/lib/hooks.js:75:12)\n    at done (/var/www/photo-app/node_modules/loopback-datasource-juggler/lib/validations.js:484:25)\n    at /var/www/photo-app/node_modules/loopback-datasource-juggler/lib/validations.js:558:7\n    at ModelConstructor.<anonymous> (/var/www/photo-app/node_modules/loopback-datasource-juggler/lib/validations.js:357:5)\n    at allCb (/var/www/photo-app/node_modules/loopback-datasource-juggler/lib/dao.js:1483:7)\n    at /var/www/photo-app/node_modules/loopback-connector-mysql/node_modules/loopback-connector/lib/sql.js:1061:14\n    at cbForWork (/var/www/photo-app/node_modules/loopback-datasource-juggler/lib/observer.js:150:34)\n    at /var/www/photo-app/node_modules/loopback-connector-mysql/node_modules/loopback-connector/lib/sql.js:413:7"
  }
}

以下是该模型的样子:

"name": "people",
  "base": "User",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "number",
      "required": true
    },
    "username": {
      "type": "string",
      "required": true
   },
    "email": {
      "type": "string"
    },
    "password": {
      "type": "string",
      "required": true
    },
    "created": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

0 个答案:

没有答案