部署应用程序时,Meteor 数据库 find() 不返回文档 [英] Meteor database find() doesn't return documents when app deployed

查看:34
本文介绍了部署应用程序时,Meteor 数据库 find() 不返回文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 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'](一些数组)替换了 helper 中的 billers.find(),它再次正常工作.所以我将问题缩小到 billers.find() 在部署时没有按预期工作,可能无法连接到数据库.虽然这不起作用,但在浏览器控制台中,我看到以下消息:
WebSocket 连接到 'wss://ddp--3592-biller2.meteor.com/sockjs/969/mty9hj18/websocket' 失败:在建立连接之前 WebSocket 已关闭.cbd6db56612e463370fc8f0b4c909d896b48d176.js:32 WebSocket 连接到 'wss://ddp--5246-biller2.meteor.com/sockjs/849/wr1l0s5w/websocket' 失败之前:WebSocket 连接建立失败:Web 连接已建立.不知道此错误消息是否与我面临的问题有关.有关如何解决此问题的任何想法?
更新:
我在下面的评论中提到的帮助器添加了一行: if(billers.find().count()==0) console.log("zero") else console.log("not zero")

This works correctly when deployed to localhost. However, when I deploy to subdomain.meteor.com using meteor deploy, the div inside #each biller is shown empty - no content. I then replaced the billers.find() in the helper with return ['a','b'] (some array), and it worked fine again. So I've narrowed the problem to the fact that billers.find() is not working as expected when deployed, maybe its unable connect to the database. While this is not working, in the browser console I see the following message :
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. I don't know if this error message is even related to the problem I'm facing . Any ideas on how to fix this?
Update:
I added a line to the helper as mentioned in the comment below: if(billers.find().count()==0) console.log("zero") else console.log("not zero")

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

When run on localhost, it gives "not zero", and when run on subdomain.meteor.com it prints "zero". So I guess, find() is working but it fetches zero documents. Why is this so?

推荐答案

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

You need to "prime" the database. That means that you create what's called a "fixture".

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

So in your /server directory create a file called "fixtures.js". In it write:

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

也变了

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

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

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

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

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

Where your capital letter signifies your miniMongo queries (client side) and your lowercase signifies your mongo queries (server side).

这篇关于部署应用程序时,Meteor 数据库 find() 不返回文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆