(类型错误:无法在 HttpHeaders.applyUpdate 处读取 null 属性“长度”)Angular 5,Http 客户端

2023-12-30

我在服务中发出 http 请求时收到此响应。

这是登录组件

export class LoginComponent {
  credentials: Credentials;

  constructor(
    private auth: AuthService,
    @Inject(UserService) private userService: UserService,
    @Inject(HttpClient) private http: HttpClient,
    private auth0: Auth0Service
  ) {}

  onLogin(credentials) {
    const sendReq = this.userService.login(credentials);
}

我在 App.module 的提供者中添加了 UserService

这是用户服务

@Injectable()
export class UserService {
  constructor(
    @Inject(Auth0Service) private authService: Auth0Service,
    protected http: HttpClient // @Inject(HttpClient) protected http: HttpClient
  ) {}
  login(credentials): Observable<any> {
    console.log(credentials);
    console.log(credentials.username);
    this.http
      .post("http://localhost:3000/api/Users/login", {
        username: credentials.username,
        password: credentials.password
      })
      .subscribe(
        data => {
          console.log(data);
        },
        err => {
          console.log("Something went wrong!");
          console.log(err);
        }
      );
    return credentials;
    // this.auth.login(credentials);
  }
}

这是控制台

    at MergeMapSubscriber._innerSub (mergeMap.js:138)
    user.service.ts:14 
{username: "hamzakpt", password: "hamza"}
    user.service.ts:15
 hamzakpt
    user.service.ts:26 Something went wrong!

    user.service.ts:27
 TypeError: Cannot read property 'length' of null
        at HttpHeaders.applyUpdate (http.js:308)
        at eval (http.js:255)
        at Array.forEach (<anonymous>)
        at HttpHeaders.init (http.js:255)
        at HttpHeaders.forEach (http.js:354)
        at Observable.eval [as _subscribe] (http.js:2153)
        at Observable._trySubscribe (Observable.js:172)
        at Observable.subscribe (Observable.js:160)
        at subscribeToResult (subscribeToResult.js:23)
        at MergeMapSubscriber._innerSub (mergeMap.js:138)

同样的 get 请求在 LoginComponent 中有效,但在 UserService 中无效。 UserService 是全局服务,而 Login 位于不同的模块中。

我还在 app.module 和登录模块中添加了 HTTPClientModule。


不要在标头中设置 Null 值

此问题可能是由于标头中的空值而发生的,即

cloned = req.clone({
    headers: req.headers.append('token', token) // i.e. token is null
})

要解决此问题,请在设置到标头之前检查变量,即

if (token) {
    cloned = req.clone({
        headers: req.headers.append('token', token)
    })
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

(类型错误:无法在 HttpHeaders.applyUpdate 处读取 null 属性“长度”)Angular 5,Http 客户端 的相关文章

随机推荐

  • 遍历批处理文件中的文件夹和文件?

    这是我的情况 项目的目标是将一些附件迁移到另一个系统 这些附件将位于父文件夹中 比方说 Folder 0 see 这个问题的图 https serverfault com questions 147902 windows command l
  • PHP:简单的正则表达式来匹配长度?

    我正在创建一个注册系统 需要使用 REGEX 并且更喜欢 检查姓名 通行证等 到目前为止我得到的是 Check so numbers aren t first such as 00foobar preg match d a z0 9 iD
  • “@reify”有什么作用以及何时使用?

    我在用户体验设计金字塔教程 http docs pylonsproject org projects pyramid tutorials en latest humans creatingux step06 index html 我不太明白
  • 使用 Angular-cli 在 Angular2 中进行本地化

    Angular 有没有本地化工具2使用时Angular cli 我遇到了这个链接 https devblog dymel pl 2016 11 03 angular2 and i18n translate your app 从 11 月开始
  • 安装 AllegroServe

    我目前正在阅读 Peter Seibel 的书 Practical Common Lisp 正在阅读第 26 章 Web 编程 在第 366 页 它说 第一步是将 AllegroServe 代码加载到 Lisp 图像中 在 Allegro
  • Android 上的 libcurl CURLE_SSL_CACERT_BADFILE 错误

    所以我尝试将 libcurl 与 JNI 一起使用 但它返回 CURLE SSL CACERT BADFILE 错误 这是我的代码 JNI 端 static size t WriteCallback void contents size t
  • Tkinter 在 Frames 中添加菜单栏

    我正在使用此处答案中发布的代码在 tkinter 中的两个框架之间切换 https stackoverflow com questions 7546050 switch between two frames in tkinter作者 Bry
  • Javascript:如何删除for循环中的最后一个逗号

    如何从此函数中删除最后一个逗号 for var i 0 i lt 100 i if i 2 0 div innerHTML l else div innerHTML 首先 使用数组而不是字符串来求和字符串 这样速度更快 第二 var arr
  • 如何制作长度为5的随机数字和字母字符串? [复制]

    这个问题在这里已经有答案了 可能的重复 这是生成随机字符字符串的好方法吗 https stackoverflow com questions 976646 is this a good way to generate a string of
  • jQuery ui Sortable 可以表现得像 gridster 吗?

    是否可以得到jQuery ui 可排序网格 http jqueryui com sortable display grid表现得像gridster http gridster net 为什么不使用网格斯特 因为它不适用于 IE9 以下的任何
  • 如何使用 ggrepel 抑制警告

    ggrepel允许通过排斥彼此太近的标签来避免文本标签重叠 该算法取决于查看窗口大小 并且当窗口大小更改时会发生回调 如果算法无法避免给定窗口大小的重叠 则会发出警告 Warning messages 1 ggrepel 178 unlab
  • 转换函数指针

    我正在编写一个函数 它接收一个指向比较函数的指针和一个数组MyStructs并应该根据比较函数对数组进行排序 void myStructSort struct MyStruct arr int size int comp const str
  • 更改 iOS XCode 项目中的产品名称宏

    我在上面构建了我的 iPhone 代码苹果的示例项目 http developer apple com library ios samplecode LazyTableImages Introduction Intro html 当应用程序
  • UTF8 与宽字符?

    我似乎在思考某些事情时遇到了一些困难 我正在尝试创建一个 C 函数来将 UTF8 转换为 Wide 我开始谷歌搜索 发现了 Boost 和 ICU 两者看起来都太大了 然后我找到了 utf cpp 头库 看起来不错 我通过这里的一些线程发现
  • 将文本设置为整数值

    我想设置一个TextView作为整数的值 希望像这样 tv setText int 我尝试了这个 但出现了这个错误 另外 我的整数值在另一个类中 D AndroidRuntime Shutting down VM E AndroidRunt
  • 使用 python-pptx 添加项目符号列表

    我正在使用 python pptx 库进行 pptx 操作 我想在 pptx 文档中添加项目符号列表 我使用以下代码片段添加列表项 p text frame add paragraph run p add run p level 0 run
  • 如何只获取一次脚本

    所以这是一个简化的问题 我将其用作参考 以使这个问题变得简单 如果超过 x 像素 则使用 resize 来 getScript jQuery https stackoverflow com questions 11388247 using
  • 记录 mongoose 在应用程序中触发的所有查询

    我有使用nodejs和mongodb的应用程序 我曾使用猫鼬进行ODM 现在我想记录猫鼬在整个应用程序期间触发的所有查询 如何记录这些 您可以像这样启用调试模式 mongoose set debug true 或添加您自己的调试回调 mon
  • 从视觉 API 预览条码扫描仪的尺寸

    我正在使用 Google 的 Android Vision API 中的条形码阅读器示例 预览尺寸似乎没有填满整个可用空间 我使用的是 Nexus 4 预览右侧有一个白色的未使用空间 大约是宽度的 1 3 我希望能够在各种设备上运行这个示例
  • (类型错误:无法在 HttpHeaders.applyUpdate 处读取 null 属性“长度”)Angular 5,Http 客户端

    我在服务中发出 http 请求时收到此响应 这是登录组件 export class LoginComponent credentials Credentials constructor private auth AuthService In