将GraphQL与MongoDB连接时为空

时间:2018-07-03 12:14:18

标签: mongodb graphql

我将GraphQL与MongoDB连接,但结果为 PdfReader reader; //instantiate AcroFields acroFields = reader.getAcroFields(); Map<String, Item> fieldNames = acroFields.getFields(); Set<Entry<String, Item>> entries = fieldNames.entrySet(); Iterator<Entry<String, Item>> it = entries.iterator(); while(it.hasNext()){ Entry<String, Item> entry = it.next(); //Check flags boolean invisible = isInvisible(entry.getValue()); boolean mandatory = isMandatory(entry.getValue()); boolean noView = isNoView(entry.getValue()); boolean hidden = isHidden(entry.getValue()); ... } public static boolean isInvisible(Item item) { //Add a nullcheck! if(item.size()>0) { PdfDictionary d = item.getMerged(0); PdfNumber num = (PdfNumber) d.get(PdfName.F); return num == null ? false : ((num.intValue() & PdfAnnotation.FLAGS_INVISIBLE) == PdfAnnotation.FLAGS_INVISIBLE); } return false; } public static boolean isMandatory(Item item) { //Add a null check here! if(item.size()>0) { PdfDictionary d = item.getMerged(0); PdfNumber num = (PdfNumber) d.get(PdfName.FF); return num == null ? false : ((num.intValue() & PdfFormField.FF_REQUIRED) == PdfFormField.FF_REQUIRED); } return false; } public static boolean isNoView(Item item) { //nullcheck! if(item.size()>0) { PdfDictionary d = item.getMerged(0); PdfNumber num = (PdfNumber) d.get(PdfName.F); return num == null ? false : ((num.intValue() & PdfAnnotation.FLAGS_NOVIEW) == PdfAnnotation.FLAGS_NOVIEW); } return false; } public static boolean isHidden(Item item) { //Nullcheck! if(item.size()>0) { PdfDictionary d = item.getMerged(0); PdfNumber num = (PdfNumber) d.get(PdfName.F); return num == null ? false : ((num.intValue() & PdfAnnotation.FLAGS_HIDDEN) == PdfAnnotation.FLAGS_HIDDEN); } return false; } 。帮我 !

index.js文件:

null


main.js文件:

const { graphql } = require('graphql');
const { MongoClient } = require('mongodb');
const assert = require('assert');
const readline = require('readline');

const mySchema = require('./schema/main.js');
const rli = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
const MONGO_URL = 'mongodb://127.0.0.1:27017/test';

MongoClient.connect(MONGO_URL, { useNewUrlParser: true }, (err, db) => {
    assert.equal(null, err);
    console.log('Connected to MongoDB server');
    rli.question('Client Request: ', inputQuery => {
        graphql(mySchema, inputQuery, {}, { db }).then(result => {
            console.log('Server Answer: ', result.data);
            db.close(() => rli.close());
        });
    });
});


这是结果:

const {
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLInt
} = require('graphql');

const queryType = new GraphQLObjectType({
    name: 'RootQuery',
    fields: {
        usersCount: {
            type: GraphQLInt,
            resolve: (_, args, { db }) => db.collection('users').count()
        }
    }
});

const mySchema = new GraphQLSchema({
    query: queryType
});

module.exports = mySchema;

1 个答案:

答案 0 :(得分:0)

据我了解,从db连接回调中获得的MongoClient对象不是您的db对象,而是client对象,因此 您应该先致电

const db = client.db

请参阅Connect to MongoDB

如果您可以调试代码,就可以轻松找到并修复它