tsed: [BUG] POST params and files not receive body param empty

Information

  • Version: 6.102.8
  • Packages:

Add params and files in controller not receive body (empty)

Example

@Post('/')
  @Status(201)
  @Returns(201, Product).ContentType('application/json')
  @Returns(401, Unauthorized).Description('Unauthorized')
  @Returns(403, Forbidden).Description('Forbidden')
  async addProduct(
    @Description('Produit à ajouter')
    @Required()
    @BodyParams('product', Product)
    product: Product,
    @Description('Images du produit')
    @MultipartFile('images', 5)
    images: PlatformMulterFile[]
  ): Promise<Product[]> {
    **console.log(product);** <== empty product
    // console.log(images);
    let imagesProducts: ImagesProduct[] = [];

    for (const key in images) {
      if (Object.prototype.hasOwnProperty.call(images, key)) {
        const image = images[key];
        const urlImage = path.basename(image.path);
        // console.log(urlImage);

        const mimetype: MimesTypes|undefined = await this.mimesTypesService.findOne({
          where: {
            typeMIME: image.mimetype,
          },
        });

        if (mimetype) {
          imagesProducts.push({
            data: urlImage,
            mimeType: mimetype,
          });
        }
      }
    }

    // console.log(imagesProducts);
    product.images = imagesProducts;

    console.log(product instanceof Product);
    console.log(product);

    return this.productService.save([product]);
  }

log receive param :

Product {
  id: undefined,
  images: undefined,
  title: undefined,
  description: undefined,
  category: undefined,
  brand: undefined,
  size: undefined,
  price: undefined,
  weight: undefined,
  user: undefined
}

Error :

{
  "name": "QueryFailedError",
  "message": "ER_NO_DEFAULT_FOR_FIELD: Field 'title' doesn't have a default value",
  "status": 500,
  "errors": [],
  "stack": "QueryFailedError: ER_NO_DEFAULT_FOR_FIELD: Field 'title' doesn't have a default value\n    at QueryFailedError.TypeORMError [as constructor] (/home/pkbp512/www/VEEP/back_end/src/error/TypeORMError.ts:7:9)\n    at new QueryFailedError (/home/pkbp512/www/VEEP/back_end/src/error/QueryFailedError.ts:9:9)\n    at Query.<anonymous> (/home/pkbp512/www/VEEP/back_end/src/driver/mysql/MysqlQueryRunner.ts:183:37)\n    at Query.<anonymous> (/home/pkbp512/www/VEEP/back_end/node_modules/mysql/lib/Connection.js:526:10)\n    at Query._callback (/home/pkbp512/www/VEEP/back_end/node_modules/mysql/lib/Connection.js:488:16)\n    at Query.Sequence.end (/home/pkbp512/www/VEEP/back_end/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)\n    at Query.ErrorPacket (/home/pkbp512/www/VEEP/back_end/node_modules/mysql/lib/protocol/sequences/Query.js:92:8)\n    at Protocol._parsePacket (/home/pkbp512/www/VEEP/back_end/node_modules/mysql/lib/protocol/Protocol.js:291:23)\n    at Parser._parsePacket (/home/pkbp512/www/VEEP/back_end/node_modules/mysql/lib/protocol/Parser.js:433:10)\n    at Parser.write (/home/pkbp512/www/VEEP/back_end/node_modules/mysql/lib/protocol/Parser.js:43:10)\n    at Protocol.write (/home/pkbp512/www/VEEP/back_end/node_modules/mysql/lib/protocol/Protocol.js:38:16)\n    at Socket.<anonymous> (/home/pkbp512/www/VEEP/back_end/node_modules/mysql/lib/Connection.js:88:28)\n    at Socket.<anonymous> (/home/pkbp512/www/VEEP/back_end/node_modules/mysql/lib/Connection.js:526:10)\n    at Socket.emit (events.js:400:28)\n    at addChunk (internal/streams/readable.js:293:12)\n    at readableAddChunk (internal/streams/readable.js:267:9)"
}

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20

Most upvoted comments

Yes that works now. Thanks for rapidly to help me

Can you try this:

@Injectable({
   priority: -1
})
export class ParseBodyJsonPipe implements PipeMethods<string> {
  transform(value: string, metadata: JsonParameterStore): string {
      console.log('ParseBodyJsonPipe', typeof value, value);
      
    const val = JSON.parse(value);
   
    console.log(val)
    return val;
  }
}