Meteor 服务器上未捕获的TypeError,但在客户端上有效
当我运行此命令时:Meteor 服务器上未捕获的TypeError,但在客户端上有效,meteor,Meteor,当我运行此命令时: var vtop=Posts.findOne({},{sort: {created_at:-1},reactive:false}).created_at; console.log(vtop); 它出现了一个错误:uncaughttypeerror:无法读取未定义的属性'created_at',但当我运行Posts.findOne{},{sort:{created_at:-1},reactive:false}。created_at;在web控制台上,它会产生预期的结
var vtop=Posts.findOne({},{sort: {created_at:-1},reactive:false}).created_at;
console.log(vtop);
它出现了一个错误:uncaughttypeerror:无法读取未定义的属性'created_at',但当我运行Posts.findOne{},{sort:{created_at:-1},reactive:false}。created_at;在web控制台上,它会产生预期的结果。我会在代码行上设置一个断点,然后在JavaScript引擎即将运行时查看Posts集合中的内容。我的猜测是,您的Posts集合在遇到该行代码后,正在加载文档。当您使用web控制台检查时,它工作的原因是因为此时文档已加载。在调用数据之前,您需要确保订阅已完成 在meteor中,数据是通过网络发送的,因此当javascript/html被发送下来时,浏览器还没有被告知从服务器下载数据 有两种方法: 如果您还没有达到使用应用程序订阅的阶段,则可以使用Deps.autorun 使用Deps 或者等待订阅完成 服务器js 客户端js 如果使用“发布/订阅”,则需要删除“自动发布”包,但如果执行此操作,则还需要发布其他收藏,否则浏览器将看不到它们 有关如何使用发布的更多信息,请参阅文档: parties示例还使用publish并有一个屏幕广播:
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)
}
Meteor.publish("posts", function() {
return Posts.find();
}
Meteor.subscribe("posts",function() {
var vtop = Posts.findOne({},{sort: {created_at:-1},reactive:false}).created_at;
console.log(vtop)
});