{"id":2859,"date":"2014-02-26T22:33:24","date_gmt":"2014-02-26T22:33:24","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/26\/how-do-i-subscribe-to-a-model-instance-in-sails-js-collection-of-common-programming-errors\/"},"modified":"2014-02-26T22:33:24","modified_gmt":"2014-02-26T22:33:24","slug":"how-do-i-subscribe-to-a-model-instance-in-sails-js-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/02\/26\/how-do-i-subscribe-to-a-model-instance-in-sails-js-collection-of-common-programming-errors\/","title":{"rendered":"How do I subscribe to a model instance in Sails.JS?-Collection of common programming errors"},"content":{"rendered":"<p>I am attempting to use the subscribe function described here. However, when editing <code>\/assets\/js\/app.js<\/code>, I am getting this error:<\/p>\n<pre><code>Uncaught ReferenceError: Room is not defined \n<\/code><\/pre>\n<p>So, I am not entirely sure why, but it cannot find my model. Here is my code:<\/p>\n<pre><code>Room.subscribe(req, [{id: \"5278861ab9a0d2cd0e000001\"}], function (response) {\n  console.log('subscribed?');\n  console.log(response);\n});\n<\/code><\/pre>\n<p>and here is is in the context of app.js<\/p>\n<pre><code>(function (io) {\n\n  \/\/ as soon as this file is loaded, connect automatically, \n  var socket = io.connect();\n  if (typeof console !== 'undefined') {\n    log('Connecting to Sails.js...');\n  }\n\n  socket.on('connect', function socketConnected() {\n\n    \/\/ Listen for Comet messages from Sails\n    socket.on('message', function messageReceived(message) {\n\n      \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n      \/\/ Replace the following with your own custom logic\n      \/\/ to run when a new message arrives from the Sails.js\n      \/\/ server.\n      \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n      log('New comet message received :: ', message);\n      \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n    });\n\n\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ Here's where you'll want to add any custom logic for\n    \/\/ when the browser establishes its socket connection to \n    \/\/ the Sails.js server.\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    log(\n        'Socket is now connected and globally accessible as `socket`.\\n' + \n        'e.g. to send a GET request to Sails, try \\n' + \n        '`socket.get(\"\/\", function (response) ' +\n        '{ console.log(response); })`'\n    );\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n    \/\/ This is the part I added: \n    Room.subscribe(req, [{id: \"5278861ab9a0d2cd0e000001\"}], function (response) {\n      console.log('subscribed?');\n      console.log(response);\n    });\n    \/\/\n\n\n   });\n\n\n  \/\/ Expose connected `socket` instance globally so that it's easy\n  \/\/ to experiment with from the browser console while prototyping.\n  window.socket = socket;\n\n\n  \/\/ Simple log function to keep the example simple\n  function log () {\n    if (typeof console !== 'undefined') {\n      console.log.apply(console, arguments);\n    }\n  }\n\n\n})(\n<\/code><\/pre>\n<p>Am I going about this the right way? should I be storing this <em>directly in<\/em> app.js?<\/p>\n<ol>\n<li>\n<p>You are mixing server side code with browser code. Sails only provides some extensions to the socket object that let you call your controllers easily. Here are the docs: link.<\/p>\n<p>You need to handle your subscribtion inside your controllers and call them from the browser.<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>I am attempting to use the subscribe function described here. However, when editing \/assets\/js\/app.js, I am getting this error: Uncaught ReferenceError: Room is not defined So, I am not entirely sure why, but it cannot find my model. Here is my code: Room.subscribe(req, [{id: &#8220;5278861ab9a0d2cd0e000001&#8221;}], function (response) { console.log(&#8216;subscribed?&#8217;); console.log(response); }); and here is is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2859","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2859"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2859\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}