【问题标题】:Meteor database find() doesn't return documents when app deployed部署应用程序时流星数据库 find() 不返回文档
【发布时间】:2015-12-18 10:03:00
【问题描述】:

我有以下 HTML:

<template name="something">
  {{#each biller}}
    <div>write_something</div>
  {{/each}}
</template> 

#each biller 的助手如下:

billers = new Mongo.Collection("billers");

Template.something.helpers({
  biller: function(param){
    return billers.find({param:param}) ;
  }
});

这在部署到 localhost 时可以正常工作。但是,当我使用 meteor deploy 部署到 subdomain.meteor.com 时,#each biller 内的 div 显示为空 - 没有内容。然后我用return ['a','b'](一些数组)替换了助手中的billers.find(),它又可以正常工作了。所以我把问题缩小到billers.find()在部署时没有按预期工作,也许它无法连接到数据库。虽然这不起作用,但在浏览器控制台中我看到以下消息:
WebSocket connection to 'wss://ddp--3592-biller2.meteor.com/sockjs/969/mty9hj18/websocket' failed: WebSocket is closed before the connection is established. cbd6db56612e463370fc8f0b4c909d896b48d176.js:32 WebSocket connection to 'wss://ddp--5246-biller2.meteor.com/sockjs/849/wr1l0s5w/websocket' failed: WebSocket is closed before the connection is established. 我不知道此错误消息是否与我面临的问题有关。有关如何解决此问题的任何想法?
更新:
我在下面的评论中提到的助手中添加了一行:if(billers.find().count()==0) console.log("zero") else console.log("not zero")

在本地主机上运行时,它给出“非零”,而在 subdomain.meteor.com 上运行时,它打印“零”。所以我猜,find() 正在工作,但它获取零个文档。为什么会这样?

【问题讨论】:

  • 您的应用程序中是否存在夹具数据?有关更多信息,我要问的是您是否有信息说 if (CollectionName.find().count() === 0 ) { //insert data } ?
  • @thatgibbyguy 谢谢,用这个细节更新了问题。

标签: node.js mongodb meteor


【解决方案1】:

不确定为什么您的代码在 localhost 中有效,因为您要查找的输入应该是字符串或对象。这是collection.find documentation

我认为你的意思是这样写你的代码

Template.something.helpers({
  biller: function(param){
    return billers.find({param:param});
  }
});

【讨论】:

  • 抱歉,这是问题中的错字。这就是我用代码编写的方式。那不是问题。更新了问题。
【解决方案2】:

您需要“准备”数据库。这意味着您创建了所谓的“夹具”。

因此,在您的 /server 目录中创建一个名为“fixtures.js”的文件。在里面写:

if (Billers.find().count() === 0)
    Billers.insert({
       "billerName":"Testing"        
    })

也改变

billers = new Mongo.Collection("billers");

Billers = new Mongo.Collection("billers");

您的大写字母表示您的 miniMongo 查询(客户端),您的小写字母表示您的 mongo 查询(服务器端)。

【讨论】:

    猜你喜欢
    • 2015-08-16
    • 1970-01-01
    • 2014-03-27
    • 2016-06-24
    • 2019-10-16
    • 2015-06-29
    • 2017-05-14
    • 2012-05-28
    • 2019-02-03
    相关资源
    最近更新 更多