user
,状态为transactions
,this.store.dispatch( new actions.Query());
的动作导航到页面时,我在Query()
中调度了ngOnInit()
动作,该动作调用了一个Effect,该效果从数据库中检索交易并将其添加到商店中。
这在第一次加载页面时效果很好,但是从该页面导航并再次返回会从我的商店中删除user
状态。
我确定此函数调用是罪魁祸首,因为删除它可以确保用户状态保留在商店中。
我该如何解决此问题,或者有人可以解释一种调试此问题的好方法吗?我正在努力寻找能够覆盖商店整体状态的任何地方。
这是我的ngOnInit呼叫
ngOnInit(): void {
this.transactions = this.store.select(fromTransaction.selectAll);
this.store.dispatch( new actions.Query());
}
这是我的交易效果
@Effect()
query$: Observable<any> = this.actions$.pipe(
// listens for when the query action is called
ofType(transactionActions.QUERY),
// takes the action and returns a reference of the database with a snapshot of changes
switchMap(action => {
return this.firestore.collection<Transaction>('transactions', ref => {
return ref;
})
.snapshotChanges();
}),
mergeMap(actions => actions),
map(action => {
return {
type: `[TRANSACTION] ${action.type}`,
payload: {
...action.payload.doc.data(),
id: action.payload.doc.id
}
};
})
);
这是我的交易减少器:
export function TransactionReducer(
state: State = initialState,
action: actions.TransactionActions
) {
switch (action.type) {
case actions.ADDED:
return transactionAdapter.addOne(action.payload, state);
case actions.MODIFIED:
return transactionAdapter.updateOne({
id: action.payload.id,
changes: action.payload
}, state);
case actions.REMOVED:
console.log(action.payload);
return transactionAdapter.removeOne(action.payload.id, state);
default:
return state;
}
}
这是状态界面:
export interface AppState {
transaction: Transaction[];
user: User | null;
}
这是User Reducer
export function userReducer(state: User = defaultUser, action: Action) {
switch (action.type) {
case userActions.GET_USER:
return { ...state, loading: true };
case userActions.AUTHENTICATED:
return { ...state, ...action.payload, loading: false };
case userActions.NOT_AUTHENTICATED:
return { ...state, ...defaultUser, loading: false};
case userActions.GOOGLE_LOGIN:
return { ...state, loading: true};
case userActions.AUTH_ERROR:
return { ...state, ...action.payload, loading: false};
case userActions.LOGOUT:
return { ...state, loading: true};
}
}
这是我在app.module.ts
中导入的StoreModule
StoreModule.forRoot({
transaction: TransactionReducer,
user: userReducer
}),
答案 0 :(得分:1)
在交换机中添加默认值。请记住,减速器必须返回新状态,并且它始终接收所有操作。如果reducer由于操作与任何#Connect to SQL and run QUERY
$SQLServer = "xxxx"
$SQLDBName = "xxxx"
$SQLUsername = "xxxx"
$SQLPassword = "nxxx"
$OuputFile = "c:\SQL_Export.csv"
$SqlQuery = "SELECT
rtrim(HANDLE) as Handle,
rtrim(EMPRESA) as Empresa,
rtrim(FILIAL) as Filial,
rtrim(OPERACAO) as Operacao,
rtrim(PESSOA) as Pessoa,
rtrim(dataemissao) as Dataemissao,
rtrim(documentodigitado) as Documentodigitado,
rtrim(dataultimaliq) as Dataultimaliq,
rtrim(ehprevisao) as Ehprevisao,
rtrim(status) as Status,
rtrim(entradasaida) as Entradasaida
FROM [$SQLDBName].[dbo].[FN_DOCUMENTOS]
where DATAULTIMALIQ >= '20200101'
ORDER BY handle ASC"
##Delete the output file if it already exists
If (Test-Path $OuputFile ){
Remove-Item $OuputFile
}
Write-Host "INFO: Exporting data from $SQLDBName to $OuputFile" -foregroundcolor white -backgroundcolor blue
## - Connect to SQL Server using non-SMO class 'System.Data':
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; User ID = $SQLUsername; Password = $SQLPassword"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
#Output RESULTS to CSV
$DataSet.Tables[0] | select-Object -ExcludeProperty Dataemissao @{Name="Dataemissao";Expression={([datetime]$_.Dataemissao).ToString("yyyy-MM-dd HH:mm:ss.fff")}}, * | Export-Csv $OuputFile
(Get-Content $OuputFile) | Foreach-Object {$_ -replace '"', ""} | Set-Content $OuputFile -Encoding `UTF8`
不匹配而没有返回任何内容,就好像它在返回case
作为您的新状态一样。
所以:
undefined
顺便说一句,请小心点差操作员。它仅返回状态的浅表副本。如果状态中存储了复杂的对象,那么您将打破一条黄金法则:不变性。我建议您在需要时使用immerjs来建立一个新状态。