上传临时素材时执行上传时提示如下错误,错误发生在request内部,不知是什么原因引起,求帮忙
来源:
caryjohn
2016-04-26
TypeError: source.on is not a function at Function.DelayedStream.create (C:\Users\jphu\work\dianshi\node_modules\delayed-stream\lib\delayed_stream.js:33:10) at FormData.CombinedStream.append (C:\Users\jphu\work\dianshi\node_modules\combined-stream\lib\combined_stream.js:43:37) at FormData.append (C:\Users\jphu\work\dianshi\node_modules\form-data\lib\form_data.js:68:3) at appendFormValue (C:\Users\jphu\work\dianshi\node_modules\request\request.js:341:21) at Request.init (C:\Users\jphu\work\dianshi\node_modules\request\request.js:352:11) at new Request (C:\Users\jphu\work\dianshi\node_modules\request\request.js:142:8) at request (C:\Users\jphu\work\dianshi\node_modules\request\index.js:55:10) at tryCatcher (C:\Users\jphu\work\dianshi\node_modules\bluebird\js\release\util.js:16:23) at ret (eval at <anonymous> (C:\Users\jphu\work\dianshi\node_modules\bluebird\js\release\promisify.js:184:12), <anonymous>:14:23) at C:\Users\jphu\work\dianshi\wechat\wechat.js:94:29 at tryCatcher (C:\Users\jphu\work\dianshi\node_modules\bluebird\js\release\util.js:16:23) at Promise._settlePromiseFromHandler (C:\Users\jphu\work\dianshi\node_modules\bluebird\js\release\promise.js:502:31) at Promise._settlePromise (C:\Users\jphu\work\dianshi\node_modules\bluebird\js\release\promise.js:559:18) at Promise._settlePromiseCtx (C:\Users\jphu\work\dianshi\node_modules\bluebird\js\release\promise.js:596:10) at Async._drainQueue (C:\Users\jphu\work\dianshi\node_modules\bluebird\js\release\async.js:143:12) at Async._drainQueues (C:\Users\jphu\work\dianshi\node_modules\bluebird\js\release\async.js:148:10)
2回答
-
Scott
2016-04-26
可以把 调用 uploadMaterial 方法的代码也贴一下么?
然后,在 request(options) 前面,把 options 也打印一下:
console.log(options)
看看是什么内容,记得 request 在使用之前,是需要 promisify 一下的:
var request = Promise.promisify(require('request'))
00 -
caryjohn
提问者
2016-04-26
附上上传素材的代码:
Wechat.prototype.uploadMaterial = function(type,material,permanent) {
var that = this;
var form = {};
var uploadUrl = api.temporary.upload;
var _=require('lodash');
if(permanent){
uploadUrl = api.permanent.upload;
_.extend(form, permanent);
}
if(type === 'pic'){
uploadUrl = api.permanent.uploadImg;
}
if(type === 'news'){
uploadUrl = api.permanent.uploadNews;
form = material;
}
else{
form = fs.createReadStream(material);
}
console.log('上传素材的form如下:\n');
console.log(form);
return new Promise(function(resolve,reject){
that.fetchAccessToken()
.then(function(data){
var url = uploadUrl + '&access_token=' + data.access_token;
if(!permanent){
url += '&type=' + type;
}else{
form.access_token = data.access_token;
}
var options = {
method:'POST',
url:url,
json:true
}
if(type === 'news')
{
options.body = form;
}else{
options.formData = form;
}
request(options).then(function(res){
var _data = res.body;
if(_data){
resolve(_data);
}else{
throw new Error('Upload material failed.')
}
})
.catch(function(err){reject(err) })
})
});
}
00
相似问题