Saturday, 27 May 2017

Error: Unknown string to Buffer converter for option: 11

I have make this function in order to build a coap request:

function buildCoapRequest(req){
    console.log('Host:'+req.url.host)
    var url = req.url.protocol+'://'+req.url.host
    console.log('url:'+url)
    var coapReq = coap.request(url)

    if(req.url.basePath || req.url.pathName){
        var uriPath = req.url.basePath || ''
        var paramsPath = ''

        if(req.url.pathName){
            uriPath = uriPath + req.url.pathName
        }

        if(req.parameterPath){
            req.parameterPath.forEach(function(e){
                paramsPath += e
            })            
            uriPath += pramsPath
        }
        console.log('uriPath:'+uriPath)
       coapReq.setOption("11",new Buffer('test'))
    }


    if(req.headers.accept){
        coapReq.setOption('Accept',req.headers.accept)
    }

    return coapReq
}

When the function is executed I get the error:

(node:3624) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Unknown string to Buffer converter for option: 11

The strange things is that when I run the code:

const coap  = require('coap') // or coap
    , req   = coap.request('coap://coap.me')

req.setOption("11",new Buffer('test'))
req.on('response', function(res) {
  res.pipe(process.stdout)
})

req.end()

It work and don't return me any error, despite both the code use the instruction req.setOption("11",new Buffer('test')).

Do you know what is wrong in the first function?



via SimCor

No comments:

Post a Comment