-1

我试图在 graphql 查询时返回一个 mongodb 文档,但得到空值。没有显示错误。mongodb 查询适用于 mongoshell 或 mongoose。

这是架构、类型定义和解析器:

const unionSchema = new Schema(
  {
    geometry: mongoose.Schema.Types.MultiPolygon,
    properties: {
      Divi_name: String,
      Dist_name: String,
      Upaz_name: String,
      Uni_namae: String,
    },
  },
  { collection: "unionbounds" }
);


const union = mongoose.model("Union", unionSchema);


const typeDefs = `
  type Query {  
    data: Union
  }
  type Union {
    properties: Props
  }
  type Props{
    Dist_name: String,
  }

`;

const resolvers = {
  Query: {
    data: () => {
      union.findOne(
        {
          geometry: {
            $geoIntersects: {
              $geometry: {
                type: "Point",
                coordinates: [90, 22],
              },
            },
          },
        },
        "properties"
      );
    },

  },
};

Mongoshell 查询返回文档:

{
  properties: {
    Div_ID: '10',
    Dist_ID: '04',
    Upz_ID: '28',
    Un_ID: '95',
    Un_UID: '10042895',
    Divi_name: 'Barisal',
    Dist_name: 'Barguna',
    Upaz_name: 'Barguna Sadar Upazila',
    Uni_name: 'Naltona',
    Area_SqKm: 45.7658667915
  },
  _id: 6001e54a51c6d49215322f94
}

我怀疑我在解析器功能中做错了什么。我将不胜感激任何建议。

4

1 回答 1

0

问题确实是解析器功能。以下代码在从回调函数返回结果并使用 async .. await 后工作。

const resolvers = {
  Query: {
    data: async () => {
      var value;
      await union.findOne(
        {
          geometry: {
            $geoIntersects: {
              $geometry: {
                type: "Point",
                coordinates: [90, 22],
              },
            },
          },
        },
        "properties",
        function (err, result) {
          value = result;
        }
      );
      return value;
    },   

  },
};
于 2021-01-17T02:37:30.867 回答