Typescript mon goose - неопределенный объект в моем массиве - PullRequest
0 голосов
/ 20 июня 2020

Я не понимаю, почему мое обещание не выполняется, если я понимаю: мои объекты, созданные из моих документов mongodb, должны заполнять мой массив, и здесь он дает undefined, поэтому мой объект правильно пуст?

product.resolver.ts

import { Resolver, Query, Arg, Int, ObjectType, Mutation } from 'type-graphql';
import { Product, ProductModel, ProductResponse } from './product.type';
import { filterItems, getRelatedItems } from '../../helpers/filter';
import { createProductSamples } from './product.sample';

@Resolver()
export class ProductResolver {
  private readonly items: Product[] = createProductSamples();

  @Query(() => Product)
  async product(
    @Arg('slug', (type) => String) slug: string
  ): Promise<Product | undefined> {
    return await this.items.find((item) => item.slug === slug);
  }

}

product.sample.ts

Вот функция для получения данных из mongodb и заполнения моего массива объектами документов

import { Product, ProductModel }  from './product.type';
import { plainToClass } from 'class-transformer';
import { useQuery } from '@apollo/react-hooks';
import gql from 'graphql-tag';

export function createProductSamples() {
  //const inventory = useQuery<InventoryData>(GetInventory);
  const data = ProductModel.find();
  console.log(data);
  return plainToClass(Product, [data]);

}

product.type.ts

вот соответствующий класс

@ObjectType()
export class Product {

  @Field()
  @Property()
  id: number;

  @Field()
  @Property()
  slug: string;

  @Field()
  @Property()
  title: string;

  @Field(() => ProductType)
  @Property()
  type: ProductType;

  @Field(() => [Category])
  @Property()
  categories: Category[];

  @Field()
  @Property()
  unit: string;

  @Field()
  @Property()
  image: string;

  @Field(() => [Gallery])
  @Property()
  gallery: Gallery[];

  @Field()
  @Property()
  description: string;

  @Field()
  @Property()
  price: number;

  @Field()
  @Property()
  salePrice: number;

  @Field()
  @Property()
  discountInPercent: number;

  @Field(() => Author, { nullable: true })
  @Property()
  author?: Author;

  @Field(() => Meta, { nullable: true })
  @Property()
  meta?: Meta;

  @Field()
  @Property()
  createdAt: Date;
}

Выходной GraphQl

{
  "errors": [
    {
      "message": "Cannot read property 'Promise' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "product"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: Cannot read property 'Promise' of undefined",
            "    at new NativeCollection ............................
            ...............................
          ]
        }
      }
    }
  ],
  "data": null
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...