为什么ID返回null而不返回数据库中的下一个ID?

为什么ID返回null而不从数据库返回下一个ID?我认为该ID应该是数据库中可用的下一个ID。相反,它只是返回一个空值。以下是我的组件。每当console.log(this.vehicle.id)时,我都会得到NaN,并且在浏览器上,该值为null。我需要一些帮助找出我做错了什么。

export class VehicleFormComponent implements OnInit {
  makes: any;
  models: any[];
  features: any;
  vehicle: SaveVehicle = {
    id: 0,makeId: 0,modelId: 0,isRegistered: false,features: [],contact: {
      name: '',email: '',phone: ''
    }
  };
  constructor(
    private route: activatedRoute,private router: Router,private vehicleService: VehicleService,private toastrService: ToastrService) {
    route.params.subscribe(p => {
      this.vehicle.id = +p['id'];
    });
  }

  ngOnInit() {
    console.log(this.vehicle.id.toFixed(1));
    const sources = [
      this.vehicleService.getMakes(),this.vehicleService.getFeatures(),];
    if (this.vehicle.id) {
      sources.push(this.vehicleService.getVehicle(this.vehicle.id));
    }
    forkJoin(sources).subscribe((data: any) => {
      this.makes = data[0];
      this.features = data[1];
      if (this.vehicle.id) {
        this.setVehicle(data[2]);
        this.populateModels();
      }
    },err => {
      if (err.status == 404) {
        this.router.navigate(['/']);
      }
    });
  }
  private setVehicle(v: Vehicle) {
    this.vehicle.id = v.id;
    this.vehicle.makeId = v.make.id;
    this.vehicle.modelId = v.model.id;
    this.vehicle.isRegistered = v.isRegistered;
    this.vehicle.contact = v.contact;
    this.vehicle.features = _.pluck(v.features,'id');
  }
  onmakeChange() {
    this.populateModels();
    delete this.vehicle.modelId;
  }
  private populateModels() {
    const selectedMake = this.makes.find((m: { id: number; }) => m.id == this.vehicle.makeId);
    this.models = selectedMake ? selectedMake.models : [];
  }
  onFeatureToggle(featureId: number,$event: { target: { checked: any; }; }) {
    if ($event.target.checked) {
      this.vehicle.features.push(featureId);
    } else {
      const index = this.vehicle.features.indexOf(featureId);
      this.vehicle.features.splice(index,1);
    }
  }
  submit() {
    if (this.vehicle.id) {
      this.vehicleService.update(this.vehicle)
        .subscribe(x => {
          this.toastrService.success('The vehicle was sucessfully updated.','Success');
        });
    } else {
      this.vehicleService.create(this.vehicle)
        .subscribe(x => console.log(x));
    }
  }

这是我为要添加到数据库中的新车创建的端点

    [HttpPost]
    [Authorize]
    public async Task<IactionResult> CreateVehicle([FromBody] SaveVehicleResource vehicleResource)
    {
        if (!ModelState.IsValid)
            return BadRequest(ModelState);

        var vehicle = mapper.Map<SaveVehicleResource,Vehicle>(vehicleResource);
        vehicle.LastUpdate = DateTime.Now;

        repository.Add(vehicle);
        await unitOfWork.CompleteAsync();

        vehicle = await repository.GetVehicle(vehicle.Id);

        var result = mapper.Map<Vehicle,VehicleResource>(vehicle);

        return Ok(result);
    }

这是SaveVehicleresouce.cs

public class SaveVehicleResource
{
    public int Id { get; set; }
    public int ModelId { get; set; }
    public bool IsRegistered { get; set; }

    [Required]
    public ContactResource Contact { get; set; }
    public ICollection<int> Features { get; set; }

    public SaveVehicleResource()
    {
        Features = new Collection<int>();
    }
}

这是我正在使用的接口。我将int设为null吗?

export interface Vehicle {
  id: number;
  model: keyvaluepair;
  make: keyvaluepair;
  isRegistered: boolean;
  features: keyvaluepair[];
  contact: Contact;
  lastUpdate: string;
}

export interface SaveVehicle {
  id?: number;
  modelId: number;
  makeId: number;
  isRegistered: boolean;
  features: number[];
  contact: Contact;
}
zzf200215053 回答:为什么ID返回null而不返回数据库中的下一个ID?

尝试移动

route.params.subscribe(p => {
  this.vehicle.id = +p['id'];
});

放入ngOnInit(),然后将逻辑已经放在订阅中。

如果ngOnInit()生命周期事件在route.params可观察事件触发之前触发,则将尚未设置ID。

,

更新,我要做的就是更改

route.params.subscribe(p => {
      this.vehicle.id = +p['id'];
    });

route.params.subscribe(p => {
      this.vehicle.id = +p['id'] || 0;
    });
本文链接:https://www.f2er.com/3162989.html

大家都在问