I'm trying to check if a certain object ( this
) is a stream instance. I'm having a hard time to identify if it's still the original this
deep down in the function as it was when the function fired.
I have tried typeof this
and it returns an object
. I have looked on this matter and did not find a clear answer.. any suggestions? Thanks before
StreamName.prototype._getEndpointData = function ( endpoint ) {
/*
Make a request based on a specific endpoint
*/
var apikey = this.source.apikey;
request.getAsync( {
headers: {
// 'content-type' : 'application/x-www-form-urlencoded',
'User-Agent': 'request',
'Authorization': getAuthByApikey( apikey )
},
url: generateUrl( apikey, endpoint )
})
.get( 1 ) // get the body
.then( function ( body ) {
if ( body == 'Authentication Failed' ) {
throw new Error( body );
}
return body;
})
.then( JSON.parse )
.then( function ( body ) {
if ( body.status == 500 ) {
throw new Error( body.message || 'MailChimp API request error');
}
// collections: lists, campaigns & reports
var collection = body[ endpoint ];
for (var i in collection ){
var instanceEndpoint = endpoint + '/' + collection[i].id;
request.getAsync( {
headers: {
'User-Agent': 'request',
'Authorization': getAuthByApikey( apikey )
},
url: generateUrl( apikey, instanceEndpoint )
})
.get( 1 ) // get the body
// .then( console.log)
.then( function ( body ) {
if ( body == 'Authentication Failed' ) {
throw new Error( body );
}
return body;
})
.then( JSON.parse )
.then( function ( body ) {
return body;
})
.then( this.push.bind( this ) )
// Getting an error Unhandled rejection TypeError: Cannot read property 'bind' of undefined
// IS 'THIS' as the same as it was at top of the function?
.then( this.push.bind( this, null ) )
.catch( this.emit.bind( this, 'error' ) );
}
})
}
You should use the instanceof
operator:
var stream = require('stream');
var isStream = this instanceof stream.Readable;
although there could be some other issues with this, you could read about them here: nodejs: Check if variable is readable stream
this
here refers to request.getAsync
.then( this.push.bind( this, null ) )
You need to save this
reference to some variable in the beginning of the function, then you can refer to it later
StreamName.prototype._getEndpointData = function ( endpoint ) {
/*
Make a request based on a specific endpoint
*/
var apikey = this.source.apikey;
var _stream = this;
request.getAsync( {
headers: {
// 'content-type' : 'application/x-www-form-urlencoded',
'User-Agent': 'request',
'Authorization': getAuthByApikey( apikey )
},
url: generateUrl( apikey, endpoint )
})
.get( 1 ) // get the body
.then( function ( body ) {
if ( body == 'Authentication Failed' ) {
throw new Error( body );
}
return body;
})
.then( JSON.parse )
.then( function ( body ) {
if ( body.status == 500 ) {
throw new Error( body.message || 'MailChimp API request error');
}
// collections: lists, campaigns & reports
var collection = body[ endpoint ];
for (var i in collection ){
var instanceEndpoint = endpoint + '/' + collection[i].id;
request.getAsync( {
headers: {
'User-Agent': 'request',
'Authorization': getAuthByApikey( apikey )
},
url: generateUrl( apikey, instanceEndpoint )
})
.get( 1 ) // get the body
// .then( console.log)
.then( function ( body ) {
if ( body == 'Authentication Failed' ) {
throw new Error( body );
}
return body;
})
.then( JSON.parse )
.then( function ( body ) {
return body;
})
.then( _stream.push.bind( this ) )
//should replace this with _stream
.then( _stream.push.bind( this, null ) )
.catch( _stream.emit.bind( this, 'error' ) );
}
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.