dfu*_*cci 4 handlebars.js meteor
我想试试Meteor,所以我说在coffeescript中开发一个小型的多房间聊天应用程序.我有问题使用把手将findOne的结果传递给html页面.
if Meteor.is_client
room=Rooms.findOne({id:1})
Template.room({room_name:room.name})
Run Code Online (Sandbox Code Playgroud)
在html页面中
<head>
<title>Chat!</title>
</head>
<body>
{{> room}}
</body>
<template name="room">
Welcome to {{room_name}}
</template>
Run Code Online (Sandbox Code Playgroud)
现在,假设id = 1的房间文档名称='Room1',我希望页面呈现'Welcome to Room1'但是有一个白页,控制台显示2个错误:
Uncaught TypeError: Cannot read property 'name' of undefined
Uncaught TypeError: Cannot read property 'room_name' of undefined
Run Code Online (Sandbox Code Playgroud)
即使该文件确实存在,显然房间也未定义.
dan*_*nny 11
在客户端数据库缓存有时间同步到服务器之前,它在一瞬间未定义.一旦客户端同步,模板应该再次渲染,但是因为它在第一次不会发生错误时抛出错误(我最近对类似的问题感到困惑).
试试这个(使用短路&&测试该房间存在):
if Meteor.is_client
Template.room.room_name = ->
room = Rooms.findOne({id:1})
room && room.name
Run Code Online (Sandbox Code Playgroud)
注意:我将findOne调用移动到函数中以确保在更新发生时调用它,但是在你拥有它的时候也可能没问题