Никакие ответы на подобные вопросы не помогли мне
ФОН:
Я настраивал соединение с SQL Server из своего приложения Ionic. Смотрите мой предыдущий вопрос для context
Ionic подключается к ASP.NET MVC и подключается к SQL Server.
Мне удалось получить записи для добавления в базу данных SQL, где свойство id автоматически увеличивается, но свойство name всегда равно NULL, несмотря на то, что я передаю строку для свойства через ion-input.
СООБЩЕНИЕ ОБ ОШИБКЕ:
ОШИБКА TypeError: Невозможно прочитать свойство 'name' из неопределенного
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()))
}
}
Почему я получаю это сообщение об ошибке, что я сделал не так? Любая помощь приветствуется.