。map()中catch块中的变量未更新。

编程入门 行业动态 更新时间:2024-10-16 00:20:13

。map()中catch块中的<a href=https://www.elefans.com/category/jswz/34/1771380.html style=变量未更新。"/>

。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块中的变量未更新。

本文发布于:2024-05-07 16:00:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757037.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   map   catch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!