{"action":"opened","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3568","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3568/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3568/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3568/events","html_url":"https://github.com/Automattic/mongoose/issues/3568","id":116410172,"number":3568,"title":"What is the recommended way to populate some of a model's attributes automatically based on provided attributes?","user":{"login":"lgomez","id":20891,"avatar_url":"https://avatars.githubusercontent.com/u/20891?v=3","gravatar_id":"","url":"https://api.github.com/users/lgomez","html_url":"https://github.com/lgomez","followers_url":"https://api.github.com/users/lgomez/followers","following_url":"https://api.github.com/users/lgomez/following{/other_user}","gists_url":"https://api.github.com/users/lgomez/gists{/gist_id}","starred_url":"https://api.github.com/users/lgomez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lgomez/subscriptions","organizations_url":"https://api.github.com/users/lgomez/orgs","repos_url":"https://api.github.com/users/lgomez/repos","events_url":"https://api.github.com/users/lgomez/events{/privacy}","received_events_url":"https://api.github.com/users/lgomez/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-11-11T19:59:38Z","updated_at":"2015-11-11T19:59:38Z","closed_at":null,"body":"Hi all,\r\n\r\nI'm writing some models that contain attributes for which values come from external services. These values are fetched from those services using some attributes provided by the instantiator of the model. I'll give you an example:\r\n\r\nAn Address model. The user will provide an address string and, optionally, a zipcode. If we don't get a zipcode, we use a service to get it from the address. Once we do have the zipcode, we use a different service to get the country and state codes. To illustrate:\r\n\r\n```javascript\r\nvar user = new User({\r\n    \"address\": \"123 main st\",\r\n    \"zipcode\": \"90210\"\r\n});\r\n```\r\nNow, BEFORE saving, I need to trigger a an HTTP call that fetches the state and country codes so I can do:\r\n```javascript\r\nconsole.log(user.state); // to check the state code\r\nconsole.log(user.country); // to check the country code\r\n```\r\nI suspect the answer is in a post init hook but an example would be awesome because I haven't been able to get it to work. Any ideas?\r\n\r\nKeep in mind that I may or may not need to check those values so it is possible that I would be calling `save()` immediately after instantiating... Which tells me there may be a race condition there where `save()` may be called before the country and state codes are received from the external services.\r\n\r\nThank you in advance.\r\n"}}