变量未更新。"/>
。map()中catch块中的变量未更新。
同级向导,[我正在使用.map()迭代对象数组,经过一些操作并将值赋给已定义的变量后,我将它们插入了postgres数据库中
const insertIntoDB = (jsonObj) => {
let client = connectDB()
let customerId = null
let shippingName = null
let shippingZip = null
let firstOrder = null
let lastOrder = null
let isReturning = null
let query = {
text = `INSERT INTO ${tableFullName}(customer_id, shipping_name, shipping_zip, first_order, last_order, is_returning) VALUES($1, $2, $3, $4, $5, $6)`,
values = []
}
await Promise.all( jsonObj.map((item) => {
customerId = item.customerId
shippingName = item.shippingName
shippingZip = item.shippingZip
firstOrder = item.firstOrder
lastOrder = item.lastOrder
isReturning = false
if(item.orders > 1) isReturning = true
client.query(query)
.then(() => {
console.log('Inserted',customerId)
})
.catch((err) => {
// here customerId gets the same value where exception was thrown for the all iterations
// but err reports duplicate key for each customerId
console.log(customerId, err)
})
})
})
在映射过程中,当对数据库中的重复项引发异常时,customerId的值保持不变,即引发第一个错误。
error: duplicate key value violates unique constraint "customers_customer_id_key"
at Parser.parseErrorMessage
length: 242,
name: 'error',
severity: 'ERROR',
code: '23505',
detail: 'Key (customer_id)=(5374657068616e696520) already exists.',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'customers',
column: undefined,
dataType: undefined,
constraint: 'customers_customer_id_key',
file: 'nbtinsert.c',
line: '570',
routine: '_bt_check_unique'
} {
text: 'INSERT INTO public.customers(customer_id, shipping_name, shipping_zip, first_order, last_order, is_returning) VALUES($1, $2, $3, $4, $5, $6)',
values: [
'4a756c6965204772696e', // <= CustomerId
'Julie Grindstaff',
"'37128",
'8/15/19 18:44',
'8/15/19 18:44',
false
]
}
如果您发现CustomerId的值和详细信息不同。
同伴向导,我使用.map()迭代对象数组,经过一些操作并将值赋给已定义的变量,然后将它们插入postgres数据库const insertIntoDB =(...
回答如下:更多推荐
。map()中catch块中的变量未更新。
发布评论