Wednesday 3 May 2017

mobile-detect.js - works with string but not request header

I'm using mobile-detect.js with Node.js to get the user agent.

When I use the test string in the documentation, it works fine, but doesn't seem to do anything when I get the string from my request header.

  md = new MobileDetect(req.headers['user-agent']);
  console.log(req.headers['user-agent']);
  console.log(md.userAgent() );

First console.log returns my query string (Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36) and second one returns null. In other words, the library doesn't seem to work.

However, when I use the test string it works fine.

  md = new MobileDetect('Mozilla/5.0 (Linux; U; Android 4.0.3; en-in; SonyEricssonMT11i' +
' Build/4.1.A.0.562) AppleWebKit/534.30 (KHTML, like Gecko)' +
' Version/4.0 Mobile Safari/534.30'););
  console.log(md.userAgent() );

That returns Safari as expected.

So there must be a problem with the type of data I'm feeding to it, but I'm not sure what that problem is.



via sanjaypoyzer

No comments:

Post a Comment