TypeError:undefined不是nodejs中管道的函数

时间:2015-07-30 11:50:43

标签: javascript node.js

我正在尝试从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);
)};

1 个答案:

答案 0 :(得分:2)

试试这个:

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);
});