【问题标题】:Error unhandledPromiseRejectionWarning while making a SOAP connection in nestJS在 nestJS 中建立 SOAP 连接时出错 unhandledPromiseRejectionWarning
【发布时间】:2021-06-14 15:40:53
【问题描述】:
import { Controller, Get } from '@nestjs/common';
import { EventPattern } from '@nestjs/microservices';
import { InjectModel } from '@nestjs/mongoose';
import { json } from 'express';
import { Model } from 'mongoose';
import { stringify } from 'querystring';
import { XmlUtils } from 'src/xml.utils';
import * as soap from 'soap';
import {product} from './product/product.model';

@Controller()
export class AppController {
  XmlUtils:XmlUtils;



  constructor(  @InjectModel('product') private readonly productModel: Model<any>,) { }

  @EventPattern( "super" )
  async handleMessagePrinted(data: Record<any, any>) {
    let dataXML;
  this.insertproduct(data);  

    this.sendSoap(data);

  }

  async insertproduct(data:any) {
    stringify(data);
    this.productModel.insertMany(data);

}

async sendSoap(data:any){
const soapRequest = require('easy-soap-request');
const fs = require('fs');

// example data
let url:string = 'http://soapapu.com';
let sampleHeaders = {
  'user-agent': 'sampleTest',
  'Content-Type': 'text/xml;charset=UTF-8',
  'soapAction': 'http://soapapu.com',
};
let xml = fs.readFileSync('./product/product.xml', 'utf-8');

// usage of module
(async () => {
  const { response } = await soapRequest({ url: url, headers: sampleHeaders, xml: xml, timeout: 1000 }); // Optional timeout parameter(milliseconds)
  const { headers, body, statusCode } = response;
  console.log(headers);
  console.log(body);
  console.log(statusCode);
})();
}


}

我得到 unhandledPromiseRejectionWarning 的任何想法:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。要在未处理的 Promise 拒绝时终止节点进程,请使用 CLI 标志 --unhandled-rejections=strict(请参阅 https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝编号:10)?

【问题讨论】:

    标签: node.js soap nestjs


    【解决方案1】:

    您收到此警告是因为您通过不等待或返回您的承诺而破坏了承诺链 3 次。为了解决这个问题,你必须等待,或者返回每一个承诺,或者通过调用他们的 catch 方法来处理他们的拒绝。

    这是您的代码的固定版本:

    // ...
    @Controller()
    export class AppController {
      XmlUtils: XmlUtils;
    
      constructor(
        @InjectModel("product") private readonly productModel: Model<any>
      ) {}
    
      @EventPattern("super")
      async handleMessagePrinted(data: Record<any, any>) {
        let dataXML;
        // IMPORTANT: here we want to wait for this method, because it returns a promise.
        await this.insertproduct(data);
        // IMPORTANT: we want to delegate the handle of this promise so we return it.
        return this.sendSoap(data);
      }
    
      async insertproduct(data: any) {
        stringify(data);
        // IMPORTANT: we want to return this promise, se the caller can wait for it.
        return this.productModel.insertMany(data);
      }
    // ...
    

    我建议您阅读some guide,了解有关 promise-chains 和 async-await 功能的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      相关资源
      最近更新 更多