GraphQL查询返回空数组,但MongoDB查询有效

时间:2019-09-24 03:25:11

标签: javascript python mongodb graphql pymongo

编辑: 我发现该问题的解决方案是设置collection参数。 在mongoDB中,crypto_debug没有以复数形式出现。真是头疼。

PyMongo和Mongoose之间的集合自动命名有所不同:

例如:

inserting to db using mongoose:
bandcampDebugModel = db2.model('bandcamp_debug', bandcampDebugSchema);

becomes 'bandcamp_debugs'

inserting to db using PyMongo:
db2 = client['cryptoDebug']
debug = db2.crypto_debug

remains 'crypto_debug'

The solution was to add the explicit collection name:

const cryptoDebugModel = crypto_debug.model("crypto_debug",
    {
        name : String,
        dateOfIssue: String,
        error: String
    }, "crypto_debug");

我遇到一个问题,其中graphQL查询返回一个空数组,并且该查询在MongoDB中正常工作。

此应用程序正在运行5个刮板脚本。 2个是用Python制造的,3个是用Javascript制造的。

在MongoDB中,总共有11个用graphQL查询的集合。 11个作品中有9个没有问题。

有问题的两个集合都使用pymongo驻留在python脚本中。 我将发布这两者之一的代码,因为两者的结构相似。

这时我真的碰壁了,并且尝试了所有我能想到的事情,并且不确定在哪里调试以找到此问题的根源。

我尝试过的事情:

  • 在mongodb中,字段的typeof是字符串,在那里看起来都很正常
  • graphQL端点中的
  • cryptoDebugModel.find()查询的不同形式
  • 在python脚本中删除日期dateOfIssue,以查看pymongo / string是否存在问题(但如果它在mongoDB中显示为字符串,那应该不是问题吗?)
  • 重新命名数据库/集合以解决可能的复数情况

就像我说的那样,在11个工作中,有9个工作没有问题,所有低落的想法都被用尽,例如错别字,重复的姓名等。

该查询在mongoDB控制台中看起来不错,但是在graphQL查询中返回了一个空数组,因此非常感谢您的帮助。

如果需要,我可以提供完整的代码,以使问题尽可能简洁。

This is an image of the mongoDB query of crypto_debug.
This is an image of the typeof for the mongoDB query of crypto_debug.
This is an image of the graphQL query for crypto_debug.


        import pymongo
        client = MongoClient()

        db = client['crypto'] # prices.update_one and prices.insert_one work fine here
        prices = db.crypto_data_prices

        db2 = client['cryptoDebug']
        debug = db2.crypto_debug

    ...
    try:
    ...
    except:
                e = str( sys.exc_info()[0] )

                name = str( 'Cryptocurrency' )
                dateOfIssue = str( "{:%B %d, %Y}".format(datetime.now()) )
                error = str( 'Error with updating database: %s' % e )

                debug.insert_one(
                    {
                        'name' : name,
                        'dateOfIssue' : dateOfIssue,
                        'error' : error
                    }
                )


const mongoose = require("mongoose");

    const crypto_debug = mongoose.createConnection('mongodb://localhost/cryptoDebug', {useNewUrlParser: true});

    const topSearches_debug = mongoose.createConnection('mongodb://localhost/googleSearchesDebug', {useNewUrlParser: true});

    Promise.all([bandcamp, crypto, newMovies, newOnSteam, topSearches, upcomingSneakers,
                 bandcamp_debug, crypto_debug, newMovies_debug, topSearches_debug, upcomingSneakers_debug]).then(() => {

    // model and type for crypto debug data
    //----------------------------------------------------------------
    const cryptoDebugModel = crypto_debug.model("crypto_debug",
        {
            name : String,
            dateOfIssue: String,
            error: String
        });



    const cryptoDebugType = new GraphQLObjectType({
            name: "crypto_debug_records", 
            fields: { 
                name: { type: GraphQLString },
                dateOfIssue: { type: GraphQLString },
                error: { type: GraphQLString }
            }
        });

// model and type for top searches debug data
//----------------------------------------------------------------
const topSearchesDebugModel = topSearches_debug.model("top_searches_debug",
    {
        name: String,
        dateOfIssue: String,
        error: String
    });



const topSearchesDebugType = new GraphQLObjectType({
        name: "top_searches_debug_records", 
        fields: { 
            name: { type: GraphQLString },
            dateOfIssue: { type: GraphQLString },
            error: { type: GraphQLString }
        }
    });

//.......

const schema = new GraphQLSchema({
        query: new GraphQLObjectType({
            name: "Query", 
            fields: {
//...
crypto_debug: {
                    type: GraphQLList(cryptoDebugType),
                    resolve: (root, args, context, info) => {
                        return cryptoDebugModel.find().sort({_id : -1}).exec();
                        }
                    },
top_searches_debug: {
                    type: GraphQLList(topSearchesDebugType),
                    resolve: (root, args, context, info) => {
                        return topSearchesDebugModel.find().sort({_id : -1}).exec();
                    }
                }, 
//.........

0 个答案:

没有答案
相关问题