尝试使用ngx-cookie获取cookie,但未定义'文档'如果我F5页面

时间:2017-12-19 21:31:45

标签: javascript angular dom cookies

有些奇怪的事情正在发生。似乎文档对象可能不存在。当我只保存代码并单击返回浏览器时,它工作正常。当我按F5时,为什么文件会消失?

 let x = this.cookieService.get("UserID");
    console.log("x= " + x);
  

处理请求时发生未处理的异常。   NodeInvocationException:Uncaught(在promise中):ReferenceError:未定义文档   ReferenceError:未定义文档   在CookieService.get [as cookieString](C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ main-server.js:10652:13)   在CookieService._cookieReader(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ main-server.js:10751:40)   在CookieService.get(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ main-server.js:10670:21)   在新的HomeComponent(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ main-server.js:16948:36)   在createClass(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:21881:26)   在createDirectiveInstance(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:21699:37)   在createViewNodes(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:23127:49)   在createRootView(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:23022:5)   在Object.createProdRootView [as createRootView](C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:23707:12)   在ComponentFactory_.module.exports.ComponentFactory_.create(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:20813:46)   错误:未捕获(在承诺中):ReferenceError:未定义文档   ReferenceError:未定义文档   在CookieService.get [as cookieString](C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ main-server.js:10652:13)   在CookieService._cookieReader(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ main-server.js:10751:40)   在CookieService.get(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ main-server.js:10670:21)   在新的HomeComponent(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ main-server.js:16948:36)   在createClass(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:21881:26)   在createDirectiveInstance(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:21699:37)   在createViewNodes(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:23127:49)   在createRootView(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:23022:5)   在Object.createProdRootView [as createRootView](C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:23707:12)   在ComponentFactory_.module.exports.ComponentFactory_.create(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:20813:46)   at resolvePromise(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:86165:31)   at resolvePromise(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:86136:17)   在C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:86213:17   在ZoneDelegate.module.exports.ZoneDelegate.invokeTask(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:85819:31)   at Object.onInvokeTask(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:14821:37)   在ZoneDelegate.module.exports.ZoneDelegate.invokeTask(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:85818:36)   在Zone.module.exports.Zone.runTask(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:85586:47)   在drainMicroTaskQueue(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ vendor.js:85979:35)   在   at process._tickCallback(internal / process / next_tick.js:188:7)   Microsoft.AspNetCore.NodeServices.HostingModels.HttpNodeInstance + d__7.MoveNext()

堆栈查询Cookie标头

  

NodeInvocationException:Uncaught(在promise中):ReferenceError:文档未定义ReferenceError:文档未在CookieService.get [as cookieString]中定义(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ main -server.js:10652:13)在CookieService._cookieReader(C:\ Users \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ main-server.js:10751:40)处于CookieService.get(C:\ Users) \ Matt_Herb \ Downloads \ IbewSurveyTemp \ IbewSurveyTemp \ ClientApp \ dist \ main-server.js:10670:21)at new

2 个答案:

答案 0 :(得分:1)

服务器端无法访问文档。 cookie管理必须是浏览器端。

答案 1 :(得分:0)

在 app.server.module.ts 文件中使用以下代码

  providers: [
    { provide: CookieService, useClass: CookieBackendService },
  ]