无论是通过引用调用还是按名称调用都不适用于动态集合名称

时间:2016-09-06 06:17:42

标签: javascript mongodb meteor

我试图动态提供集合名称并对其进行查询,如下所示:

databaseName.findOne({"type":"starsCount"})

但是,来自this tutorial的引用引用或名称调用都不起作用。

在我的夹具文件中设置数据,我调用了从github API请求数据的方法。

import { Meteor } from 'meteor/meteor';

Meteor.call('accessGitAPI','facebook','react','reactInfo')

在声明流星方法的文件中:

Meteor.methods({
    accessGitAPI:function(userName,repoName,databaseNameString){
        var databaseName = global[databaseNameString]
        HTTP.call( 'GET', `https://api.github.com/repos/${userName}/${repoName}`, 
            {
                headers: 
                {
                    'Content-Type':'application/json',
                    "Accept":"application/vnd.github.v3+json",
                    "User-Agent": "whales"
                },
            },
            function( error, response ) {
          if ( error ) {

            console.log( error );
          } else {

            let responseData = response.data;

            let starsCountAlreadyExist = databaseName.findOne({"type":"starsCount"})

            let issuesCountAlreadyExist = databaseName.findOne({'type':'issuesCount'})


          }
        });
    }

});

通过在全局对象中搜索该字符串,我最终得到了未定义的结果。

错误:

Exception in callback of async function: TypeError: Cannot read property 'findOne' of undefined

然后我决定使用call-by-reference。我直接使用数据库引用作为参数传递给函数。然后我删除了该行以在全局命名空间中搜索该对象。在夹具文件中:

import { Meteor } from 'meteor/meteor';
import {ReactInfo} from '../../api/react_info.js'

Meteor.call('accessGitAPI','facebook','react',ReactInfo)

然而,这会导致另一个错误:

RangeError: Maximum call stack size exceeded

react_info.js

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

不确定为什么两者都不起作用。该教程非常有意义,但它不起作用。我也不明白为什么在添加导入并直接传递数据库引用作为参数后发生调用堆栈错误。

0 个答案:

没有答案
相关问题