2016-08-23 190 views
-1

更新:我仍然得到了同样的问题如下,但是现在下面的错误是由JSDom发布到控制台:HERE地图API

Error: Uncaught [SyntaxError: ] 
    at reportException (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:58:24) 
    at processJavaScript (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/HTMLScriptElement-impl.js:130:7) 
    at HTMLScriptElementImpl._eval (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/HTMLScriptElement-impl.js:65:7) 
    at e (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:30:22) 
    at Object.check (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:97:11) 
    at /Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:116:12 
    at wrappedEnqueued (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:234:16) 
    at Request.request [as _callback] (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:184:9) 
    at Request.self.callback (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/request/request.js:187:22) 
    at emitTwo (events.js:106:13) 
    at Request.emit (events.js:191:7) 
    at Request.<anonymous> (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/request/request.js:1044:10) 
    at emitOne (events.js:96:13) 
    at Request.emit (events.js:188:7) 
    at Gunzip.<anonymous> (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/request/request.js:965:12) 
    at emitNone (events.js:91:20) 
    at Gunzip.emit (events.js:185:7) 
    at endReadableNT (_stream_readable.js:934:12) 
    at _combinedTickCallback (internal/process/next_tick.js:74:11) 
    at process._tickCallback (internal/process/next_tick.js:98:9) SyntaxError 
    at XMLHttpRequest.open (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:451:15) 
    at http://js.api.here.com/v3/3.0/mapsjs-core.js:51:86 
    at http://js.api.here.com/v3/3.0/mapsjs-core.js:51:383 
    at http://js.api.here.com/v3/3.0/mapsjs-core.js:290:26 
    at Object.exports.runInContext (vm.js:44:17) 
    at processJavaScript (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/HTMLScriptElement-impl.js:128:10) 
    at HTMLScriptElementImpl._eval (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/HTMLScriptElement-impl.js:65:7) 
    at e (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:30:22) 
    at Object.check (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:97:11) 
    at /Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:116:12 
    at wrappedEnqueued (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:234:16) 
    at Request.request [as _callback] (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:184:9) 
    at Request.self.callback (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/request/request.js:187:22) 
    at emitTwo (events.js:106:13) 
    at Request.emit (events.js:191:7) 
    at Request.<anonymous> (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/request/request.js:1044:10) 
    at emitOne (events.js:96:13) 
    at Request.emit (events.js:188:7) 
    at Gunzip.<anonymous> (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/request/request.js:965:12) 
    at emitNone (events.js:91:20) 
    at Gunzip.emit (events.js:185:7) 
    at endReadableNT (_stream_readable.js:934:12) 
    at _combinedTickCallback (internal/process/next_tick.js:74:11) 
    at process._tickCallback (internal/process/next_tick.js:98:9) 

我m试图让HERE Maps JavaScript API在使用Mocha编写的测试中正常工作。

it("should generate a map when the component gets rendered", function generateMapTest(done) { 
    this.timeout(0); 
    const scriptMap = getScriptMap(); 
    const scriptNames = Object.keys(scriptMap); 
    const finalScriptToLoad = last(scriptNames); 

    getScript(finalScriptToLoad).onLoad((err: any, res?: any) => { 
     global.H = window.H; 

     const platform = new H.service.Platform({ 
      app_id: 'app_id', 
      app_code: 'app_code', 
     }); 

     console.log(window.H); 
     done(err); 
    }); 
}); 

要解释上述情况 - scriptMap变量用于存储包含要加载的每个HERE Maps脚本的URL的对象。每个脚本通过使用JavaScript将标签附加到DOM(使用jsdom)来按顺序加载。当每个脚本加载时,将调用回调数组中的每个回调,并传递通常的错误和响应对象。使用下面显示的onLoad方法将回调添加到此数组。

当最终脚本从HERE服务器完成加载后,我将H对象附加到Mocha中的全局变量并尝试创建平台服务。这将导致以下错误:

Unhandled rejection TypeError: Cannot read property 'mapsjs-core' of undefined 
    at Hi (http://js.api.here.com/v3/3.0/mapsjs-core.js:287:626) 
    at Ji (http://js.api.here.com/v3/3.0/mapsjs-core.js:288:350) 
    at V.Es (eval at <anonymous> (http://js.api.here.com/v3/3.0/mapsjs-core.js:47:404), <anonymous>:13:188) 
    at new V (eval at <anonymous> (http://js.api.here.com/v3/3.0/mapsjs-core.js:47:404), <anonymous>:12:257) 
    at Object.getPlatform (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/src/utils/get-platform.js:9:16) 
    at /Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/src/HEREMap.js:34:50 
    at /Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/src/utils/cache.js:52:104 
    at arrayEach (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/lodash/lodash.js:522:11) 
    at Function.forEach (/Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/node_modules/lodash/lodash.js:9011:14) 
    at /Users/joshua/Dropbox/Projects/Web Development/NPM Modules/react-here-maps/src/utils/cache.js:52:30 

我奋力调试提供的代码的精缩版,所以如果我能有一些帮助追查这个bug的来源我将不胜感激。可能值得说明的是,所有这些都是在构建HERE Maps React组件的环境中,源代码可以在这里找到:https://github.com/josh-es/react-here-maps

〜^〜

回答

0

我设法将问题隔离到JSDom实现了XMLHttpRequest API的。用'xmlhttprequest'npm包替换它解决了这个问题。通过运行安装:

npm install --save-dev xmlhttprequest 

,然后修复上述问题:

import { XMLHttpRequest } from "xmlhttprequest"; 

global.window.XMLHttpRequest = XMLHttpRequest;