Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 异步函数在Express中工作,但在NestJs中不工作_Javascript_Node.js_Typescript_Express_Nestjs - Fatal编程技术网

Javascript 异步函数在Express中工作,但在NestJs中不工作

Javascript 异步函数在Express中工作,但在NestJs中不工作,javascript,node.js,typescript,express,nestjs,Javascript,Node.js,Typescript,Express,Nestjs,我最初创建了一个小型express服务器来运行报告和文件写入功能 var ssrs = require('mssql-ssrs'); var fs = require('fs'); const express = require('express') const app = express() const port = 3001 app.get('/', (req, res) => { reportCreation(); res.send('File Created')

我最初创建了一个小型express服务器来运行报告和文件写入功能

var ssrs = require('mssql-ssrs');
var fs = require('fs');

const express = require('express')
const app = express()
const port = 3001

app.get('/', (req, res) => {
    reportCreation();
    res.send('File Created');
})

app.get('/api', (req, res) => {
    reportCreation();
    res.json({'File Created': true});
})

app.listen(port, () => {
    console.log(`Report Api listening at http://localhost:${port}`)
})
函数reportCreation()是一个从SSRS获取报告的异步函数。这个很好用

async function reportCreation() {
var serverUrl = 'http://reportServerName/ReportServer/ReportExecution2005.asmx';

ssrs.setServerUrl(serverUrl);

var reportPath = '/ApplicationPortalReports/TestReportNew';
var fileType = 'word';
var parameters = { ApplicationId: 3, TrainingCardId: 267, PortalPersonId: 52 }

var auth = {
    username: 'USERNAME',
    password: 'PASSWORD',
    domain: 'dmz'
};

try {
    var report = await ssrs.reportExecution.getReportByUrl(reportPath, fileType, parameters, auth)
} catch (error) {
    console.log(error);
}
console.log(report);
try {
    fs.writeFile('ReportApiTest.doc', report, (err) => {
        if (!err) console.log('Data written');
    });
} catch (error) {
    console.log(error);

}
我最近一直在与NestJs合作,希望在NestJs服务中使用相同的功能

@Injectable()
export class AppService {

  async  getReport(): Promise<string> {
   
    const serverUrl = 'http://reportServerName/ReportServer/ReportExecution2005.asmx';

    ssrs.setServerUrl(serverUrl);
    const reportPath = '/ApplicationPortalReports/TestReportNew';
    const fileType = 'word';
    // var parameters = {appId: 3, ReportInstanceId: 1 }
    const parameters = {ApplicationId: 3, TrainingCardId: 267, PortalPersonId: 52 };

    const auth = {
      username: 'USERNAME',
      password: 'PASSWORD',
      domain: 'dmz'
    };

    try {
       var report = await ssrs.reportExecution.getReportByUrl(reportPath, fileType, parameters, auth)
      } catch (error) {
        console.log(error);
      }
      console.log(report);

      // excel = xlsx
      // word = doc
      // pdf = pdf
    try {
      fs.writeFile('ReportApiTest.doc', report,  (err) => {
          if (!err) { console.log('Data written'); 
        return 'File Written Succesfully'}
        });
      } catch (error) {
          console.log(error);
          return 'File Write Error'
      }
      }
  }
等待。为什么这适用于Express而不是NestJS?下面是来自NestJs的错误

buffer.js:219
  throw new ERR_INVALID_ARG_TYPE(
  ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer,
Array, or Array-like Object. Received type undefined
    at Function.from (buffer.js:219:9)
    at new Buffer (buffer.js:179:17)
    at Object.createType3Message (C:\Projects\SSRS-report-api\ssrs-report-api\node_modules\httpntlm\ntlm.js:172:19)
    at sendType3Message (C:\Projects\SSRS-report-api\ssrs-report-api\node_modules\httpntlm\httpntlm.js:77:23)
    at Immediate._onImmediate (C:\Projects\SSRS-report-api\ssrs-report-api\node_modules\httpntlm\httpntlm.js:101:4)
在mssql ssrs节点包中,getReportByURL如下所示

async function getReportByUrl(reportPath, fileType, params, auth) {
    try {
        var config = {
            binary: true, // very important
            username: auth.userName,
            password: auth.password,
            workstation: auth.workstation,
            domain: auth.domain,
            url: soap.getServerUrl()
                + "?" + (testReportPath(reportPath).replace(/\s/g, '+'))
                + "&rs:Command=Render&rs:Format=" + reportFormat(fileType)
                + formatParamsToUrl(params)
        };
    } catch (err) { report.errorHandler(err) }

    return new Promise((resolve, reject) => {
        config.url = encodeURI(config.url);
        httpntlm.post(config, function (err, res) {
            if (res.statusCode === 500) { reject(res) }
            if (err || res.statusCode !== 200) { reject(err) }
            else { resolve(res.body) }
        })
    })
}

这是app.controller.ts

@Controller()
export class AppController {
     constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): Promise<string> {
    return this.appService.getReport();
  }
}
@Controller()
导出类AppController{
构造函数(私有只读appService:appService){}
@得到()
getHello():承诺{
返回此.appService.getReport();
}
}

我在node_模块中将userName变量更改为userName时伪造了代码,但在NestJS版本中没有这样做。我忘了我已经这么做了,所以现在它可以工作了。

我在node_模块中将userName变量更改为userName时伪造了代码,但在NestJS版本中没有这样做。我忘了我已经做了,所以现在它正在工作。

这不是问题的答案。但是,在我看到您的代码之后,我可以看到如果
等待ssrs.reportExecution.getReportByUrl(reportPath,fileType,parameters,auth)
失败,您将来将面临一个错误。事实上,你看到上面的错误就是因为这个

你使用试捕的方式真的很糟糕

这是我的编码方式

@Injectable()
export class AppService {

  async  getReport(): Promise<string> {
    const serverUrl = 'http://reportServerName/ReportServer/ReportExecution2005.asmx';

    ssrs.setServerUrl(serverUrl);
    const reportPath = '/ApplicationPortalReports/TestReportNew';
    const fileType = 'word';
    // var parameters = {appId: 3, ReportInstanceId: 1 }
    const parameters = {ApplicationId: 3, TrainingCardId: 267, PortalPersonId: 52 };

    const auth = {
      username: 'USERNAME',
      password: 'PASSWORD',
      domain: 'dmz'
    };

    const report = await ssrs.reportExecution.getReportByUrl(reportPath, fileType, parameters, auth)

    return new Promise(function(resolve, reject) {
        fs.writeFile('ReportApiTest.doc', report, , function(err) {
            if (err) reject(err);
            resolve("File Created");
        });
    });
  }

这不是问题的答案。但是,在我看到您的代码之后,我可以看到如果
等待ssrs.reportExecution.getReportByUrl(reportPath,fileType,parameters,auth)
失败,您将来将面临一个错误。事实上,你看到上面的错误就是因为这个

你使用试捕的方式真的很糟糕

这是我的编码方式

@Injectable()
export class AppService {

  async  getReport(): Promise<string> {
    const serverUrl = 'http://reportServerName/ReportServer/ReportExecution2005.asmx';

    ssrs.setServerUrl(serverUrl);
    const reportPath = '/ApplicationPortalReports/TestReportNew';
    const fileType = 'word';
    // var parameters = {appId: 3, ReportInstanceId: 1 }
    const parameters = {ApplicationId: 3, TrainingCardId: 267, PortalPersonId: 52 };

    const auth = {
      username: 'USERNAME',
      password: 'PASSWORD',
      domain: 'dmz'
    };

    const report = await ssrs.reportExecution.getReportByUrl(reportPath, fileType, parameters, auth)

    return new Promise(function(resolve, reject) {
        fs.writeFile('ReportApiTest.doc', report, , function(err) {
            if (err) reject(err);
            resolve("File Created");
        });
    });
  }

getReportByUrl方法的实现在哪里?@iAviator我已经更新了这个问题。错误清楚地表明是
数组,或者类似数组的对象。接收到的类型未定义
。因此,您应该首先比较express和nestjs中的配置对象值。值中似乎存在差异。@b向导能否投票支持帮助您使用
try..catch
的注释完全不正确。如果您想记录错误,这很好,但是您必须
抛出
,或者至少
返回
,或者删除
try..catch
。最重要的是,真正狡猾地使用
var
甚至可以让你逍遥法外。你这样对自己真丢脸!getReportByUrl方法的实现在哪里?@iAviator我已经更新了这个问题。错误清楚地表明是
数组,或者类似数组的对象。接收到的类型未定义
。因此,您应该首先比较express和nestjs中的配置对象值。值中似乎存在差异。@b向导能否投票支持帮助您使用
try..catch
的注释完全不正确。如果您想记录错误,这很好,但是您必须
抛出
,或者至少
返回
,或者删除
try..catch
。最重要的是,真正狡猾地使用
var
甚至可以让你逍遥法外。你这样对自己真丢脸!删除这个问题。这并不是回答问题的方式。只需删除问题即可。这并不像人们所问的那样回答它。
@POST
async writeFile() {
    try {
        const res = await this.appService.getReport();
        return res;
    } catch(err) {
        // handle your error
    }
}