Node.js 将数据发送到具有patchvalue的角度材质的mdDatepicker
在我的fullstack项目中,我需要更新公式集中的数据,为此,我使用公式集从数据库接收数据,并使用数据库中的原始值设置输入。问题是,当我试图设置mdDatepicker的输入时,我不知道我做错了什么。这就是错误所在 错误:Datepicker:DateAdapter无法将值识别为日期对象 后端:nodejs 数据库:mysql 正面:角度4 阶级 这是编辑公式集的控件Node.js 将数据发送到具有patchvalue的角度材质的mdDatepicker,node.js,angular,Node.js,Angular,在我的fullstack项目中,我需要更新公式集中的数据,为此,我使用公式集从数据库接收数据,并使用数据库中的原始值设置输入。问题是,当我试图设置mdDatepicker的输入时,我不知道我做错了什么。这就是错误所在 错误:Datepicker:DateAdapter无法将值识别为日期对象 后端:nodejs 数据库:mysql 正面:角度4 阶级 这是编辑公式集的控件 createControlsEdit() { this.form = this.fb.group({
createControlsEdit() {
this.form = this.fb.group({
NUM_ID_EMPRESA: '',
STR_NOMBRE: ['', Validators.compose([Validators.required])],
STR_TIPO_IDENTIFICACION: ['', Validators.compose([Validators.required])],
STR_IDENTIFICACION: ['', Validators.compose([Validators.required, Validators.pattern(validaNum)])],
createdAt: '' // this is the control for date
})
}
this.service.getEmpresa(id)
.subscribe(
rs => this.empresa = rs,
er => console.log('Error:', er),
() => {
if (this.empresa.length > 0) {
this.esEdicion = true;
this.form.patchValue({
NUM_ID_EMPRESA: this.empresa[0].NUM_ID_EMPRESA,
STR_NOMBRE: this.empresa[0].STR_NOMBRE,
STR_TIPO_IDENTIFICACION: this.empresa[0].STR_TIPO_IDENTIFICACION,
STR_IDENTIFICACION: this.empresa[0].STR_IDENTIFICACION,
createdAt: this.empresa[0].createdAt // here recieve data from model
})
}
}
)
}
这是我对处方集的价值
createControlsEdit() {
this.form = this.fb.group({
NUM_ID_EMPRESA: '',
STR_NOMBRE: ['', Validators.compose([Validators.required])],
STR_TIPO_IDENTIFICACION: ['', Validators.compose([Validators.required])],
STR_IDENTIFICACION: ['', Validators.compose([Validators.required, Validators.pattern(validaNum)])],
createdAt: '' // this is the control for date
})
}
this.service.getEmpresa(id)
.subscribe(
rs => this.empresa = rs,
er => console.log('Error:', er),
() => {
if (this.empresa.length > 0) {
this.esEdicion = true;
this.form.patchValue({
NUM_ID_EMPRESA: this.empresa[0].NUM_ID_EMPRESA,
STR_NOMBRE: this.empresa[0].STR_NOMBRE,
STR_TIPO_IDENTIFICACION: this.empresa[0].STR_TIPO_IDENTIFICACION,
STR_IDENTIFICACION: this.empresa[0].STR_IDENTIFICACION,
createdAt: this.empresa[0].createdAt // here recieve data from model
})
}
}
)
}
这是我的mdDatePicker的html视图
<div class="form-group">
<md-form-field>
<input id="createdAt" formControlName="createdAt" mdInput [mdDatepicker]="picker" (click)="picker.open()" placeholder="Elija una fecha">
<md-datepicker-toggle mdPrefix [for]="picker" ></md-datepicker-toggle>
<md-datepicker #picker></md-datepicker>
</md-form-field>
</div>
谢谢在patchValue调用中用于引用date formControl的键有一个额外的“d”createdAtd:而不是createdAt:。这就是问题所在吗 编辑 关于reported,这可能是因为datepicker被分配了一个字符串值,而不是一个实际的日期对象。您可以将date控件的初始值指定为null,而不是null。这并不重要,但也不确定。然后,每当您为该控件设置新值时,请始终确保它是一个真实的日期对象,可以使用javascript库,也可以使用 因此,在您的示例中,您可以尝试进行这些更改
createControlsEdit() {
this.form = this.fb.group({
NUM_ID_EMPRESA: '',
STR_NOMBRE: ['', Validators.compose([Validators.required])],
STR_TIPO_IDENTIFICACION: ['', Validators.compose([Validators.required])],
STR_IDENTIFICACION: ['', Validators.compose([Validators.required, Validators.pattern(validaNum)])],
createdAt: null // this is the control for date (setting to null)
})
}
this.service.getEmpresa(id)
.subscribe(
rs => this.empresa = rs,
er => console.log('Error:', er),
() => {
if (this.empresa.length > 0) {
this.esEdicion = true;
// assuming your date format is 'yyyy-mm-dd', if different change this logic
const str = this.empresa[0].createdAt.split('-');
const dateObj = new Date(+str[0], +str[1] - 1, +str[2]); // doing 'month - 1' as it is zero-based
this.form.patchValue({
NUM_ID_EMPRESA: this.empresa[0].NUM_ID_EMPRESA,
STR_NOMBRE: this.empresa[0].STR_NOMBRE,
STR_TIPO_IDENTIFICACION: this.empresa[0].STR_TIPO_IDENTIFICACION,
STR_IDENTIFICACION: this.empresa[0].STR_IDENTIFICACION,
createdAt: dateObj // use the Date obj created above
})
}
}
)
}
以后在应用程序中更新此值时,您可能必须确保该值为有效的日期类型,而不仅仅是字符串。希望有帮助。谢谢,但这不是问题所在,对不起,我已经更正了问题。好的。此.empresa[0].createdAt中的值是什么?该值对应于数据库中日期类型的记录,这是一个由服务通过id搜索的值,必须由表单获取,以便日后由日期选取器编辑并再次保存。我知道日期选取器正在使用该值。“但你能举个例子说明它的价值吗?”安迪更新了我的答案