meteor - Uncaught TypeError on the server but works on the client -
when run this:
var vtop=posts.findone({},{sort: {created_at:-1},reactive:false}).created_at; console.log(vtop);
it comes error: "uncaught typeerror: cannot read property 'created_at' of undefined", when run posts.findone({},{sort: {created_at:-1},reactive:false}).created_at;
on web console comes expected result.
you need make sure subscriptions complete before call data.
in meteor data sent on wire, when javascript/html sent down browser hasn't yet been told download data server.
there 2 ways round this:
if you've not yet reached stage you're using subscriptions app can use deps.autorun
using deps
deps.autorun(function() { var subscribed = session.equals("subscribed",true); if(!subscribed && posts.find().count()) { session.set("subscribed",true); var vtop = posts.findone({},{sort: {created_at:-1},reactive:false}).created_at; console.log(vtop) }
or wait subscription finish
server js
meteor.publish("posts", function() { return posts.find(); }
client js
meteor.subscribe("posts",function() { var vtop = posts.findone({},{sort: {created_at:-1},reactive:false}).created_at; console.log(vtop) });
if using publish/subscribe need remove autopublish
package if need publish other collections or browser won't see them.
for more on how use publish see docs: http://docs.meteor.com/#publishandsubscribe
the parties example uses publish , has screencast : http://meteor.com/examples/parties
Comments
Post a Comment