I'm working with StrongLoop and Loopback to create an API. I have extended the built-in User
model to my own model called people
. I don't want the User
model to require an email like it does by default, so I followed this work around, and now when I create users with the default User
model, email isn't required. But when I'm using my people
model, it is still required. What am I missing here?
Also, when I get a strange error when I connect my people
model to my MySQL database. When I can create users just fine with it if I have the default memory connector on i.e. using db
datasource, but when I connect it to MySQL database I get:
{
"error": {
"name": "ValidationError",
"status": 422,
"message": "The `people` instance is not valid. Details: `username` is invalid (value: \"username123\"); `email` is invalid (value: \"[email protected]\").",
"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: \"[email protected]\").\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"
}
}
And here is what that model looks like:
"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": {}
}
Figured this out. First, the email was still being required because the work around is specifying the User
model to not require the email and only the User
model. I thought because I inherited from the User
model it would not require an email. You need to specifically assign that work around to your new model. In my case, people
. Like so:
module.exports = function(app) {
delete app.models.people.validations.email;
};
As for the validation errors, this was because I'm using MySQL and I didn't have all the fields of the User
model in my database. I'm told this isn't an issue with MongoDB or other NoSQL types. But if it's relational you have to include all fields of the base model as well, even if you don't use them. using the autoMigrate()
or autoUpdate()
will prevent this from happening as well.