我正在尝试解决此问题,但我无法执行传奇。 我有两个表,一个表用于发票标题,一个用于发票项目,还有一个名为“ addItemToList”的函数来存储发票标题和发票项目。 问题是,当我第一次调用函数时,saga不会一直执行作业,而函数会继续执行并调用下一个函数来存储发票项目。当我从本地存储中获取invoiceHeader值时,invoice_header_id为0,并且在发票项目表中,我存储的invoice_header_id项目为0
{"id":0,"customer_id":19,"account_number":"300-26688-22","account_date":"2020-07-18 00:00:00","payment_date":"2020-07-18 00:00:00","status":1,"price":"256.00","tax":"256.00","total":"309.76"}
react函数的代码片段是:
if(this.state.product.length !== 0 && this.state.quantity !== 0 && this.state.itemPrice !== 0 &&
this.state.itemPriceWithDsc !== 0 && this.state.profit !== 0 && this.state.itemTotal !== 0) {
let edit = false
if(this.state.items.length === 0 && JSON.parse(localStorage.getItem("input_invoice_item") === null)) {
//function to add header details in db
this.addInputInvoiceHeader()
}
let header = JSON.parse(localStorage.getItem('input_invoice_header'))
let item = {
id: this.state.id,
invoice_id: header === null ? 0 : header.id,
product: this.state.product,
quantity: parseInt(this.state.quantity),
item_price: parseFloat(this.state.itemPrice).toFixed(2),
discount: parseFloat(this.state.discount).toFixed(2),
item_price_with_dsc: parseFloat(this.state.itemPriceWithDsc).toFixed(2),
profit: parseFloat(this.state.profit).toFixed(2),
item_total: parseFloat(this.state.itemTotal).toFixed(2),
}
}
items.map(item => {
if(item.id === this.state.id) {
edit = true
}
return edit
})
if(edit) {
this.props.editItem(item)
} else {
this.props.addItemDB(item)
this.props.addItem(item)
}
我的传奇是:
export function* addInputInvoiceHeader(action) {
const response = yield call(addInputInvoiceHeader_api, action.payload)
if(!response && (!response.data || !response.message)) {
return yield put(addInputInvoiceHeader_failure('Internal server error for input invoice header'))
}
if(response.status === 200) {
localStorage.setItem('input_invoice_header', JSON.stringify(response.data))
return yield put(addInputInvoiceHeader_success(response.data))
} else {
return yield put(addInputInvoiceHeader_failure('Error for input invoice header'))
}
}
yield takeEvery(invoicesConstants.ADDINPUTINVOICEHEADER_REQUEST, addInputInvoiceHeader)