我正在尝试设置一个使用ngrx的应用程序。
当应该加载单个实体时,我很难访问返回的实体参数。
我也因无法访问实体参数而以模态形式加载实体。
首先,通过data.service
从后端调用员工列表data.service.ts:
getEngineers() {
return this.http.get<Engineer[]>(this.engineersUrl);
}
返回所有注册工程师。
一旦列出了所有工程师条目,Engineer-list.componenent.ts添加了EditEngineer函数:
editEngineer(engineer: Engineer) {
this.store.dispatch(new engineerActions.LoadEngineer(engineer.id));
this.openEditDialog(engineer);
console.log(engineer.id, 'selectedEngineersId');
}
openEditDialog({ Name, LastName, PhoneNumber, Company, Country }: Engineer)
{
const dialogRef = this.dialog.open(EgnineerEditDialogComponent, {
disableClose: true,
width: '300px',
data: {
Name, LastName, PhoneNumber, Company, Country
}
});
}
然后进行loadEngineer $()效果:
@Effect()
loadEngineer$: Observable<Action> = this.actions$.pipe(
ofType<engineerActions.LoadEngineer>(
engineerActions.EngineerActionTypes.LOAD_ENGINEER
),
mergeMap((action: engineerActions.LoadEngineer) =>
this.dataService.getEngineerById(action.payload).pipe(
map(
(engineer: Engineer) =>
new engineerActions.LoadEngineerSuccess(engineer)
),
catchError(err => of(new engineerActions.LoadEngineerFail(err)))
)
),
);
data.service呼叫getEngineerById():
getEngineerById(payload: number): Observable<Engineer> {
console.log(`${this.engineerUrl}?id=${payload}`);
return this.http.get<Engineer>(`${this.engineerUrl}?id=${payload}`);
}
从php mysql请求返回所选工程师的JSON响应:
$result = $conn->query($sql);
$emparray = $result->fetch_all( MYSQLI_ASSOC );
echo json_encode( $emparray );
下一步是由load_engineer_success
采取的。 case engineersActions.EngineerActionTypes.LOAD_ENGINEER_SUCCESS: {
console.log(action.payload.id, ' - ID of the selected engineer object');
return engineerAdapter.addOne(action.payload, {
...state,
selectedEngineerId: action.payload.id,
loading: false,
loaded: true
});
}
这里的问题是,REDUCER的控制台已记录了响应
console.log(action.payload.id, ' - ID of the selected engineer object');
返回undefined " - ID of the selected engineer object"
当游戏机记录" action.payload"返回时:
[{…}]
0:
Company: "Andys Shack"
Country: "EE"
LastName: "Franklichtenshtein "
Name: "Angus "
PhoneNumber: "123123123123 "
Registered: "2019-03-09 01:33:28"
id: "19"
__proto__: Object
length: 1
__proto__: Array(0)
和NGRX还返回错误:
entity.js:106 @ngrx/entity: The entity passed to the `selectId` implementation returned undefined. You should probably provide your own `selectId` implementation. The entity that was passed: [{…}]0: {id: "19", Name: "Angus ", LastName: "Franklichtenshtein ", PhoneNumber: "123123123123 ", Country: "EE", …}length: 1__proto__: Array(0) The `selectId` implementation: function (instance) { return instance.id; }
有效载荷是一个数组,而不是对象。
action.payload[0].id