数据保存时,出现‘record not found or changed by another user’错误信息的解决办法

数据保存时,出现‘record not found or changed by another user’错误信息的解决办法


             'record not found or changed by  another user',这个错误是我在做项目时,遇到的问题,找了一些参考才解决。如下:

所用的控件有:TDataSetProvider,TADOQuery,TClientDataSet

                       1.当 DataSetProvider.UpdateMode=upWhereAll 时,update时 where 是指定全部字段,比如,你有a、b、c 3个字段,修改了c字段,在app server中修改的命令是update ... set c=新c where a=旧a and b=旧b and c=旧c

         如果 旧的a、b、c之一 已经被其他人改掉了,那where就找不到合适的记录来修改了。所以报告“记录被其他人修改”,这个问题和“锁定”是无关的。

                      2.当 DataSetProvider.UpdateMode=upWhereKeyOnly 时,update时 where 是指定key字段,比如,

你有a、b、c 3个字段,修改了c字段,a是key field在app server中修改的命令是update ... set c=新c where a=旧a (只比较a字段)你的程序应该设计为客户修改不到主键字段的值,这样你的客户只能修改到其他字段的值,其他字段不会出现在where中,就不会出现你说的问题了。

用第2种方法解决,注意如果你的表原来没有主键的,需要双击dataset,添加你的所有field,在有唯一值的field的 ProviderFlasgs 属性里面 InKey 设置为 true (该field必须是有唯一值的,能相当于主键的,也就是能唯一确定该行记录的),TADOQuery,TClientDataSet控件里的唯一值的field字都要设置InKey为true

根据您提供的代码和描述的问题,可以推断出在调用 `save` 方法时出现了 `Target or Cache not found` 的错误。这通常是因为在查找要保存的数据对象时未能找到匹配的对象。以下是一些可能的原因和解决方法: ### 可能的原因及解决方案 1. **数据结构不一致**: - 检查 `this.data` 和 `this.cacheData` 是否始终保持一致。如果两个数组中的某个对象被意外删除或修改,可能会导致找不到匹配的对象。 - **解决方法**:在每次添加新记录或删除记录时,同步更新 `this.data` 和 `this.cacheData`。 2. **键值唯一性问题**: - 如果生成的键值(如 `this.uniqueKeyGenerator()`)不唯一或者已经存在,可能导致多个记录拥有相同的键值。 - **解决方法**:确保 `uniqueKeyGenerator` 生成的是唯一的键值。可以通过增加更多的随机性来提高唯一性。 3. **查找逻辑问题**: - 在 `save` 方法中,通过 `newData.find(item => key === item.key)` 查找目标对象。如果 `key` 或者 `item.key` 的格式有误,也可能导致找不到对象。 - **解决方法**:检查 `key` 和 `item.key` 的数据类型和格式,确保它们是相同的类型和格式。 4. **数据更新问题**: - 在 `save` 方法中,`Object.assign(targetCache, target)` 这一行用于将 `target` 对象的属性赋值给 `targetCache`。如果 `targetCache` 未正确初始化或已丢失,则会导致错误。 - **解决方法**:在每次保存前,确保 `targetCache` 存在并且与 `target` 匹配。 ### 具体代码调整 以下是针对上述可能原因的一些具体代码调整建议: #### 确保数据结构一致 ```javascript methods: { AddTable() { const newRecord = { key: this.uniqueKeyGenerator(), meterNum: '', gmType2: '', vendorCode: '', meterNum1: '', batch: '', productionDate: null, validityYears: '', iccid: '', imei: '', statu: '', depart: '', departDate: null, intakeDirection: '', ware1house: '', gauge: '', minFlow: '', maxFlow: '', maxWorkPressure: '', workVoltage: '', editable: true, isNew: true }; this.data = [...this.data, newRecord]; this.cacheData = [...this.cacheData, { ...newRecord }]; this.editingKey = newRecord.key; }, save(key) { const newData = [...this.data]; const newCacheData = [...this.cacheData]; const target = newData.find(item => key === item.key); const targetCache = newCacheData.find(item => key === item.key); if (!target || !targetCache) { console.error('Target or Cache not found for key:', key); return; } delete target.editable; this.data = newData; Object.assign(targetCache, { ...target, editable: undefined }); this.cacheData = newCacheData; this.editingKey = ''; this.$forceUpdate(); } } ``` #### 确保键值唯一性 ```javascript computed: { uniqueKeyGenerator() { return () => { const now = Date.now(); const randomPart = Math.random().toString(36).substr(2, 9); const key = `${now}-${randomPart}`; if (this.data.some(item => item.key === key)) { return this.uniqueKeyGenerator(); // 递归生成新的唯一键值 } return key; }; } }, ``` ### 总结 以上调整主要集中在确保数据结构的一致性和生成独特的键值,以避免在查找对象时出现问题。希望这些调整能够帮助您解决 `Target or Cache not found` 的问题。如果有更多具体的错误日志或代码段,请提供更多信息以便进一步诊断。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值