Javascript 如何检查对象是否为node.js流实例?

Javascript 如何检查对象是否为node.js流实例?,javascript,node.js,this,instance,Javascript,Node.js,This,Instance,我试图检查某个对象(this)是否是流实例。我很难确定它是否仍然是函数中的原始this,就像函数启动时一样 我尝试了typeof this,它返回了一个对象。 我已经考虑过这件事,没有找到明确的答案。。有什么建议吗?谢谢你 StreamName.prototype._getEndpointData = function ( endpoint ) { /* Make a request based on a specific endpoint */ var ap

我试图检查某个对象(
this
)是否是流实例。我很难确定它是否仍然是函数中的原始
this
,就像函数启动时一样

我尝试了
typeof this
,它返回了一个
对象
。 我已经考虑过这件事,没有找到明确的答案。。有什么建议吗?谢谢你

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

您应该使用
instanceof
运算符:

var stream = require('stream');
var isStream = this instanceof stream.Readable;

虽然这可能还有一些其他问题,但您可以在此处阅读:

此处指的是
请求。getAsync

.then( this.push.bind( this, null ) )
您需要在函数开头保存对某个变量的
引用,然后您可以稍后引用它

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

你能发布你的代码吗?@VsevolodGoloviznin添加了代码。。