{"action":"closed","issue":{"url":"https://api.github.com/repos/balderdashy/waterline/issues/1087","labels_url":"https://api.github.com/repos/balderdashy/waterline/issues/1087/labels{/name}","comments_url":"https://api.github.com/repos/balderdashy/waterline/issues/1087/comments","events_url":"https://api.github.com/repos/balderdashy/waterline/issues/1087/events","html_url":"https://github.com/balderdashy/waterline/issues/1087","id":94728063,"number":1087,"title":"TypeError: Cannot read property 'destroy' of undefined","user":{"login":"elennaro","id":541555,"avatar_url":"https://avatars.githubusercontent.com/u/541555?v=3","gravatar_id":"","url":"https://api.github.com/users/elennaro","html_url":"https://github.com/elennaro","followers_url":"https://api.github.com/users/elennaro/followers","following_url":"https://api.github.com/users/elennaro/following{/other_user}","gists_url":"https://api.github.com/users/elennaro/gists{/gist_id}","starred_url":"https://api.github.com/users/elennaro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elennaro/subscriptions","organizations_url":"https://api.github.com/users/elennaro/orgs","repos_url":"https://api.github.com/users/elennaro/repos","events_url":"https://api.github.com/users/elennaro/events{/privacy}","received_events_url":"https://api.github.com/users/elennaro/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-07-13T14:01:47Z","updated_at":"2015-07-14T20:38:53Z","closed_at":"2015-07-14T20:38:53Z","body":"On model *Customer.js*:\r\n\r\n````javascript\r\nmodule.exports = {\r\n\tattributes: {\r\n\t\tname: { type: 'string', required: true, size: 100 },\r\n\t\tphone: { type: 'string', unique: true, required: true, size: 15 },\r\n\t\t//Relations\r\n\t\torders: { collection: 'order', via: 'customer' }\r\n\t},\r\n\tsetPhonePrefix: function setPhonePrefix(values, prefix) {\r\n\t\tvar regexp = new RegExp('^\\\\' + (prefix || \"+995\") + '.*$');\r\n\t\tvalues.phone && !regexp.test(values.phone)\r\n\t\t\t\t\t\t&& (values.phone = \"+995\" + values.phone);\r\n\t\treturn values;\r\n\t},\r\n\tbeforeCreate: function(values, cb) {\r\n\t\tthis.setPhonePrefix(values);\r\n\t\tcb();\r\n\t},\r\n\tbeforeUpdate: function(values, cb) {\r\n\t\tthis.setPhonePrefix(values);\r\n\t\tcb();\r\n\t},\r\n\tbeforeFind: function(query, cb) {\r\n\t\tthis.setPhonePrefix(query);\r\n\t\tcb();\r\n\t}\r\n};\r\n````\r\n\r\nOn test:\r\n\r\n````javascript\r\n/* global describe, Customer */\r\nvar chai = require('chai'),\r\n\tassert = chai.assert,\r\n\tsearchCriteria = { phone: '555555555' },\r\n\tcustomerToCreate = { name: 'san', phone: '555555555' };\r\n\r\nbefore(function (done) {\r\n\tCustomer.destroy({ }, function (err) {\r\n\t\tdone(err);\r\n\t});\r\n});\r\n\r\nafter(function (done) {\r\n\tCustomer.destroy({ }, function (err) {\r\n\t\tdone(err);\r\n\t});\r\n});\r\n\r\ndescribe('Customer', function () {\r\n\r\n\tit('#findOrCreate()', function (done) {\r\n\t\tCustomer.setPhonePrefix(searchCriteria);\r\n\t\tCustomer.findOrCreate({ where: searchCriteria }, customerToCreate)\r\n\t\t\t.then(function (foundOrCreated) {\r\n\t\t\t\tassert.propertyVal(foundOrCreated, 'name', \"san\", \"Name of customer\");\r\n\t\t\t\tassert.propertyVal(foundOrCreated, 'phone', \"+995555555555\", \"Phone of customer\");\r\n\t\t\t\tCustomer.find({ where: searchCriteria })\r\n\t\t\t\t\t\t.then(function (result) {\r\n\t\t\t\t\t\t\tassert.equal(result.length, 1);\r\n\t\t\t\t\t\t\tvar customer = result[0];\r\n\t\t\t\t\t\t\tassert.propertyVal(customer, 'name', \"san\", \"Name of customer\");\r\n\t\t\t\t\t\t\tassert.propertyVal(customer, 'phone', \"+995555555555\", \"Phone of customer\");\r\n\t\t\t\t\t\t\tdone();\r\n\t\t\t\t\t\t})\r\n\t\t\t\t\t\t.catch(done);\r\n\t\t\t\t}).catch(done);\r\n\t});\r\n\r\n});\r\n````"}}