繁体   English   中英

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

[英]How do I check if an object is a node.js stream instance?

我正在尝试检查某个对象( this )是否为流实例。 我有一个困难时期,以确定它是否仍然是原来this在功能内心深处,因为它是在函数解雇。

我已经尝试过typeof this ,它返回一个object 我已经看过这个问题,没有找到明确的答案。 之前感谢

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;

尽管这样做可能还有其他问题,但您可以在此处阅读有关它们的信息: nodejs:检查变量是否可读

this在这里指的是request.getAsync

.then( this.push.bind( this, null ) )

您需要this引用保存到函数开头的某个变量中,然后可以稍后引用它

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM