GraphQL无法解决承诺

时间:2020-10-23 10:13:58

标签: typescript mongoose graphql

我正在尝试使用猫鼬函数find()并在GraphQL查询中使用数据库中的结果。它可以与findOne()一起使用,但是find()会产生GraphQL错误:“ message”:“ db.useDb(...)。collection(...)。find(...)。那么这不是一个函数” 。但是,函数getAll()在控制台中返回3种葡萄酒。总结一下:GraphQL查询getWine使用findOne()可以工作,而GraphQL查询getWines使用find()不能工作。

async function getAll() {
  var res = null;
  return await db.useDb('test').collection('wines').find().then(
    async function(doc) {
      res = doc;
      res.limit(3);
      let wines = await res.toArray();
      console.log(wines);
      return wines;
    }
  );
}

let queryType = new graphql.GraphQLObjectType({
  name: 'Query',
  fields: {
    getWines: {
      type: wineType,
      resolve: () => {
        // let all_wines = await getAll();
        // return all_wines;
        return getAll();
      }
    },
    getWine: {
      type: wineType,
      resolve: () => {
        return getOne();
      }
    },
  }
})

0 个答案:

没有答案