简体   繁体   English

流星应用程序:如何从客户端上的集合中正确检索单个文档

[英]meteor app: how to properly retrieve a single document from a collection on the client

In my meteor app I'm storing the client's unique id string in a collection called UserNavigationTracker: 在我的流星应用程序中,我将客户端的唯一ID字符串存储在一个名为UserNavigationTracker的集合中:

{
    "clientId" : "xxNfxxPGi7tqTbxc4",
    "currentWizard" : "IntroductionWizard",
    "currentStep" : "Step-1",
    "_id" : "ifBJ688upEMfzzviC"
}

As a part of the app initialization, I need to check and see if information about my client is already stored in the database. 作为应用程序初始化的一部分,我需要检查并查看有关客户端的信息是否已存储在数据库中。 I'm struggling with retrieving this record back from the collection. 我正努力从收藏集中检索此记录。 Here is what I'm doing: 这是我在做什么:

var clientIdInDatabase = UserNavigationTrackerCollection.find({"clientId": "xxNfxxPGi7tqTbxc4"});
console.log('clientIdInDatabase:  ' + clientIdInDatabase);

The browser console output from this is: clientIdInDatabase: [object Object] 浏览器控制台的输出是:clientIdInDatabase:[object Object]

Mt question is: how can I get the actual value of the clientId field from this returned Object? mt的问题是:如何从此返回的Object中获取clientId字段的实际值?

find returns a cursor, just replace it with findOne to get a single object (or undefined). find返回一个游标,只需将其替换为findOne即可得到一个对象(或未定义)。 If you ever want to get more than one document, you can get an array of them with find(...).fetch() . 如果要获取多个文档,可以使用find(...).fetch()获得它们的数组。 You can read the documentation for all of these functions in this section of the docs. 您可以在文档的此部分中阅读所有这些功能的文档。


Based on our discussion, this may work on the client to compensate for the subscription delay: 根据我们的讨论,这可能在客户端上可以弥补订阅延迟:

Tracker.autorun(function() {
  var clientId = Session.get('clientId');
  var unt = UserNavigationTrackerCollection.findOne({clientId: clientId});
  Session.set('isClientIdInDatabase', unt != null);
});

Note this assumes that clientId and isClientIdInDatabase are stored in session variables. 请注意,这假定clientIdisClientIdInDatabase存储在会话变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM