【问题标题】:uncaught typeerror cannot read property 'touppercase' of undefined Observable._subscribe未捕获的类型错误无法读取未定义的 Observable._subscribe 的属性“touppercase”
【发布时间】:2017-11-15 16:18:54
【问题描述】:

我有一个问题。我用离子框架开发移动应用程序。这是电影应用程序。我在 github 上找到了一个示例,并尝试在我的项目中实现一些功能和设计。

我有“商店”页面。当我启动项目时,我得到了错误

未捕获(承诺中):TypeError:无法读取未定义的属性“toUpperCase” TypeError:无法读取 Observable._subscribe 未定义的属性“toUpperCase”

它来自 ngOnInit 函数,但我看不出有什么问题。我最近才开始学习使用 ionic 框架、ts 和 angular 进行移动开发。我会很高兴得到任何帮助。

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { MovieService } from '../../services/movie-service';
import { MovieDetailsPage } from '../movie-details/movie-details';

@Component({
  selector: 'page-store',
  templateUrl: 'store.html'
})
 export class StorePage {

  movies: Array<any>;

     constructor(public navCtrl: NavController, private movieService: 
 MovieService) {

     }

     ngOnInit(){
        this.movieService.send('getMovies').subscribe(a => {
            console.log(a);
            this.movies = a.results.map(b => {
                return {
                    id: b.id,
                    title: b.title,
                    image: b.poster_path,
                    overview: b.overview,
                    rating: b.vote_average
                }
            })
        });
     }

     searchMovieDB(event, key) {
         if(event.target.value.length > 2) {
             this.movieService.searchMovies(event.target.value).subscribe(
                 data => {
                     this.movies = data.results; 
                     console.log(data);
                 },
                 err => {
                     console.log(err);
                 },
                 () => console.log('Movie Search Complete')
              );
         }
     } 

     itemTapped(event, movie) {
         //console.log(movie);
         this.movieService.send('getSingleMovie', movie.id).subscribe(data 
  => console.log(data));  
         this.navCtrl.push(MovieDetailsPage, {
             movie: movie
         });
     }

 }

这是我用来创建 api 请求的“MovieService”:

import { Injectable } from '@angular/core';
import {Http, Headers, RequestMethod, Request} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Injectable()
export class MovieService {

    constructor(private http:Http) {

    }

    searchMovies(movieName) {
        var url = 'http://api.themoviedb.org/3/search/movie?query=&query=' + 
encodeURI(movieName) + '&api_key=xxxxxxxxxxxxxxxxxx';

        var response = this.http.get(url).map(res => res.json());
        return response;
    }

    send(name, id?, item?) {

                let url: string,
                    body: any,
                    api_key: string = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

                if(name == 'getMovies')
                    url = 'http://api.themoviedb.org/3/movie/'+id+'?
api_key=' + api_key;
                else if (name == 'getMoviesByPage')
                    url = 'http://api.themoviedb.org/3/movie/popular?
api_key='+api_key+'&page=' + id;
                else if (name == 'getSingleMovie')
                    url = 'https://api.themoviedb.org/3/movie/'+id+'?
api_key=' + api_key;
                else if (name == 'getVideo')
                    url = 'https://api.themoviedb.org/3/movie/'+id+'/videos?
api_key=' + api_key;
                else if (name == 'getImages')
                    url = 'https://api.themoviedb.org/3/movie/'+id+'/images?
api_key=' + api_key;


                let options = {
                    url: url,
                    body: null
                };

                if (item) {
                    body = typeof item === 'string' ? item : 
 JSON.stringify(item);
                    options.body = body;
                }


                return this.http.request(new Request(options))
                    .map(res => res.json())

            }
}

【问题讨论】:

  • 我认为那段代码不是受影响的。那里没有给toUppderCase的电话。
  • 你的控制台里还有其他东西吗?您是否尝试暂时删除此订阅?
  • 你在哪里使用toUpperCase?你能把那个sn-p也加进去吗?
  • 添加了页面和服务的完整ts代码。我没有使用“toUpperCase”,也不知道为什么会出现
  • 可以添加store.html的代码吗?

标签: angular typescript ionic-framework mobile


【解决方案1】:

问题是当您使用Request 时(顺便说一句,它与Http 一起被弃用)。您只想提出获取请求,实际上不需要其他任何东西。我在您的代码中发现了一些问题,例如,当基于 id 获取单个电影时,您的应用程序会在订阅内部的映射中抛出错误,因为您从获取一部电影中返回的数据只会返回一个对象,而不是大批。所以你需要重新考虑一下!

但这里有一个基于 id 获取一部电影的示例:

send(name, id?, item?) {
  let url: string,
  api_key: string = 'xxxxxxxxxxxxxxxxxxxxxxx';

  if (name == 'getMovies') 
    url = 'https://api.themoviedb.org/3/movie/' + id + '?api_key=' + api_key

  return this.http.get(url)
    .map(res => res.json())
}

组件:

ngOnInit() {
  this.movieService.send('getMovies').subscribe(a => {
    this.movies = a // will not be an array in this case, just an object! 
  });
}

【讨论】:

  • DEMO: stackblitz.com/edit/ionic-qu29r8?file=pages%2Fhome%2Fhome.ts 我只是将演示作为评论留下,以便能够完全删除它,因为我使用了你的 api 密钥(在大多数情况下你不应该分享): )
  • 谢谢!我现在看到了不同。我将“请求”更改为“获取”(如您所说),效果很好。
  • 太棒了!很高兴听到:)
猜你喜欢
  • 2016-06-09
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 2021-12-22
  • 2015-01-06
  • 2017-07-26
相关资源
最近更新 更多