Meteor/Mongo 'findOne' 函数返回“未定义" [英] Meteor / Mongo 'findOne' function returning "undefined"

查看:23
本文介绍了Meteor/Mongo 'findOne' 函数返回“未定义"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我(显然)是初学者,第一次发帖.我正在用 Meteor 创建一个抽认卡项目.我有一个名为cardsets"的数据库,其中包含许多与此类似的文档:

I'm (obiously) a beginner, first post. I'm creating a flashcards project with Meteor. I have a db called "cardsets" that contains lots of documents that look similar to this:

{ "_id" : "asdfg00724", 
"setTitle" : "Les animaux", 
"vocab" : [ 
[ "the dog", "le chien" ],
[ "the cat", "le chat" ], 
[ "the rabbit", "le lapin" ], 
[ "the horse", "le cheval" ] ] }

我想要做的是访问集合中的单个文档,并将文档中的每个字段分配给同名的变量,并将vocab"字段作为数组引入.

What I'm trying to do is to access a single document from the collection and assign each field in the document to a variable of the same name, with the 'vocab' field being brought in as an array.

我有一个包含此内容的/imports/api/cardsets.js:

I have an /imports/api/cardsets.js that contains this:

import { Mongo } from 'meteor/mongo';
export const Cardsets = new Mongo.Collection('cardsets');

/server/main.js 包含这一行:

The /server/main.js includes this line:

import '../imports/api/cardsets.js';

并且/client/main.js 文件包括这一行:

And the /client/main.js file includes this line:

import { Cardsets } from '/imports/api/cardsets.js';

在/client/main.js 文件中,我可以直接将新文档插入"到数据库中,因此我知道在 main.js 中可以访问 Cardfiles const:

In the /client/main.js file, I am able to directly 'insert' new documents into the database, so I know the Cardfiles const is accessible in main.js:

Cardsets.insert( {language: "Chinese", course: "CHIN102"});

按预期工作.(这仅用于测试).但是,我对findOne"所做的一切都没有产生任何结果:

Works as expected. (This is only for testing). However, NOTHING I have done with the "findOne" has produced any results:

var asdf = Cardsets.findOne( { _id : "asdfg00724"});
console.log("asdf = "+asdf);

在控制台中生成asdf = undefined".

Produces "asdf = undefined" in the console.

如果我在浏览器控制台中输入Cardsets.find({}).fetch()",我会收到一个错误:Cardsets is not defined."

And if I type "Cardsets.find({}).fetch()" in the browser console, I get an error: "Cardsets is not defined."

我也试过这个:

Cardsets.findOne({ _id : "asdfg00724"}, function(err, document) {
     console.log(document.name);
     });

但这会产生一个巨大的错误,其中包含文本匹配错误:匹配失败.OneOf、Match.Maybe 或 Match.Optional 验证".

But this produces a huge error containing the text "Match error: Failed Match.OneOf, Match.Maybe or Match.Optional validation".

我意识到我可能遗漏了一件很容易的事情,但在连续工作了很多天之后,我觉得是时候寻求帮助了.

I realize that there's probably a very easy thing I'm missing, but after working on this for many days non-stop, I felt it's time to ask for help.

推荐答案

  1. find()findOne() 不接受回调作为参数,因此您的巨大错误.
  2. 除非您位于定义这些变量的函数内的断点处,否则您的变量(包括集合)将无法在控制台中使用,
  3. 很可能您在客户端通过发布订阅从服务器接收到数据之前尝试访问 Cardsets.实施实际发布和订阅后,您将能够查看订阅的 .ready() 状态以了解您的数据何时可用.
  1. find() and findOne() do not accept callbacks as parameters, hence your huge error.
  2. your variables (including collections) will not be available in the console unless you're at a breakpoint inside a function where those variables are defined,
  3. most likely you're trying to access Cardsets before the client has received that data from the server via pub-sub. Once you implement actual publications and subscriptions you'll be able to look at the .ready() state of subscriptions to know when your data is available.

这篇关于Meteor/Mongo 'findOne' 函数返回“未定义"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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