当前位置: 代码迷 >> JavaScript >> TypeError:undefined不是Node.js中管道的函数
  详细解决方案

TypeError:undefined不是Node.js中管道的函数

热度:61   发布时间:2023-06-05 10:23:59.0

我正在尝试从jira服务器下载文件并存储在我的文件中,但是我无法将其存储在我的文件中,提示以下错误:

(data.body).pipe(fs.WriteStream('file.xlsx'));

          ^

TypeError: undefined is not a function

我的nodejs代码:

var express = require('express');
var app = express();
var request = require('request');
var bodyParser     =        require("body-parser");
var fs = require('fs');
var https = require('https');

app.use(bodyParser.urlencoded({ extended: false }));
require('ssl-root-cas/latest').inject();

var credentials = 'user:pass';
var encodedCredentials = new Buffer(credentials).toString('base64')

var url = ' https://gec-jira01.example.com/secure/attachment/IWREQ-373_update.xlsx';    

request({
    "method": "GET", 
    "rejectUnauthorized": false, 
    "url": url, 
    "headers" : {"Content-Type": "application/json",
    "Authorization": "Basic"+' '+encodedCredentials}
}, function(err, data, body) {
       //console.log(data);
       (data.body).pipe(fs.WriteStream('file.xlsx'));
       console.log(data.body);
)};

尝试这个:

request({
    method: "GET", 
    "rejectUnauthorized": false, 
    "url": url, 
    "headers" : {"Content-Type": "application/json",
    "Authorization": "Basic"+' '+encodedCredentials}
}).pipe(
   fs.createWriteStream('file.xlsx')
);

或这个:

request({
    method: "GET", 
    "rejectUnauthorized": false, 
    "url": url, 
    "headers" : {"Content-Type": "application/json",
    "Authorization": "Basic"+' '+encodedCredentials}
},function(err,data,body) {
   fs.WriteStream('file.xlsx').write(body);
});
  相关解决方案