【问题标题】:ERROR TypeError: Cannot read property 'name' of undefined (Ionic 4)错误类型错误:无法读取未定义的属性“名称”(离子 4)
【发布时间】:2019-08-26 08:07:56
【问题描述】:

没有类似问题的答案对我有帮助

背景:

我一直在从我的 Ionic 应用程序配置 SQL Server 连接。请参阅我之前的问题context

Ionic 连接到 ASP.NET MVC,然后连接到 SQL Server。

我设法将记录添加到 SQL 数据库中,其中 id 属性自动递增,但 name 属性始终为 NULL,尽管我通过 ion-input 为该属性传递了一个字符串。

错误消息:

错误类型错误:无法读取未定义的属性“名称”

at Object.eval [as updateRenderer] (SqlPage.html:17)     
at Object.debugUpdateRenderer [as updateRenderer] (core.js:23936)     
at checkAndUpdateView (core.js:23311)     
at callViewAction (core.js:23547)        
at execEmbeddedViewsAction (core.js:23510)         
at checkAndUpdateView (core.js:23307)       
at callViewAction (core.js:23547)        
at execComponentViewsAction (core.js:23489)         
at checkAndUpdateView (core.js:23312)
at callViewAction (core.js:23547)

以下是相关文件,如果有其他需要查看的文件请告诉我:

sql.page.html

<ion-header>
  <ion-toolbar>
    <ion-title>sql</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content padding>
  <ion-item>
    <ion-label>Name</ion-label>
  <ion-input type="text" [(ngModel)]="id" hidden></ion-input>
  <ion-input type="text" [(ngModel)]="name"></ion-input>
  </ion-item>
  <ion-button (click)="Add()">Add</ion-button>

  <ion-list>
  <ul>
  <li *ngFor="let items of items">
    {{ item.name }}
  <ion-button (click)="Edit(item)">Edit</ion-button>
  <ion-button (click)="Delete(item)">Delete</ion-button>


  </li>
  </ul>
  </ion-list>
</ion-content>

sql.page.ts

import { Component, OnInit } from '@angular/core';
import { SqlService } from '../../services/sql.service'

@Component({
  selector: 'app-sql',
  templateUrl: './sql.page.html',
  styleUrls: ['./sql.page.scss'],
})
export class SqlPage implements OnInit {
  items=[];
  id: string;
  name: string;

  constructor(public sql: SqlService) { 
    this.getAll()
  }

  ngOnInit() {
  }

  getAll() {
    this.items=[];
    this.sql.getAll().subscribe(data=>{
      for(var i=0;i<data.length;i++){
        this.items.push(data[i]);
      }
    })

  }

  Add() {
    if(this.id==null){
    this.sql.Create(this.name).subscribe(data=>{
      this.name=name;
      this.getAll();
    })
  }else {
    this.sql.Update(this.id, this.name).subscribe(data=>{
      //this.id=null
      this.name=name;
      this.getAll();
    })
  }        
}

  Edit(item) {
    this.id = item.id
    this.name = item.name
  }

  Delete(item) {
    this.sql.Delete(item.id).subscribe(data=>{
      this.getAll()
    })

  }

}

sql.service.ts

import { Injectable } from '@angular/core';
import { Http, Headers, RequestMethod, RequestOptions } from '@angular/http';
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class SqlService {

  url:string="http://localhost:50287/api/APIDemo/"
  constructor(private http: Http) { }

getAll(){
  return this.http.get(this.url).pipe(map(res=>res.json()));
}

Create(name) {
  var body=JSON.stringify({name});
  var header=new Headers({'Content-Type':'application/json'})
  var option=new RequestOptions({method:RequestMethod.Post,headers:header})
  return this.http.post(this.url + "Posttest",body,option).pipe(map(res=>res.json()))
}

Update(id, name) {
  var body={"id":id,"name":name};
  var header=new Headers({'Content-Type':'application/json'})
  var option=new RequestOptions({method:RequestMethod.Post,headers:header})
  return this.http.post(this.url,body,option).pipe(map(res=>res.json()))
}

Read(id) {
  return this.http.get(this.url+id).pipe(map(res=>res.json()))
}

Delete(id) {
  return this.http.delete(this.url+id).pipe(map(res=>res.json()))
}

}

为什么我会收到此错误消息,我做错了什么?任何帮助表示赞赏。

【问题讨论】:

    标签: asp.net-mvc angular typescript ionic-framework


    【解决方案1】:

    我可以在这里看到两个问题:

    1 - 在*ngFor 中更改它

    let items
    

    改成

    let item 
    

    2 - 在类 SqlPage 中

    改变

      items=[];
      id: string;
      name: string;
    

      items=[];
      id: string = "";
      name: string = ""; 
    

    当你在 JavaScript 中定义一个变量时,它的默认值是undefined

    【讨论】:

    • 这消除了所有的错误,再次感谢您!
    • 很高兴它有帮助:)
    【解决方案2】:

    在构造函数中启动您的 VAR,例如:

       constructor(public sql: SqlService) { 
            this.getAll();
            this.name = "";
          }
    

    【讨论】:

    • 在你的标签中放入适当的NAME="name"
    猜你喜欢
    • 2018-03-31
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多