【问题标题】:Error and no error with the same code: Failed to execute 'put' on 'IDBObjectStore'相同代码的错误和无错误:无法在 'IDBObjectStore' 上执行 'put'
【发布时间】:2019-09-30 11:51:23
【问题描述】:

我的代码有问题,它会引发错误:Unhandled Rejection (DataError): Failed to execute 'put' on 'IDBObjectStore': Evaluating the object store's key path did not yield a value.

虽然我的朋友使用相同的代码、相同的 git url 没有这样的问题,但它的代码的全新安装给了我错误,而他的代码工作行为良好。

在 db.fieldsets.put() 上抛出错误 可能问题出在索引或主键上,但代码可以在其他设备上运行,所以我猜问题不在代码中

dexie_db.js:

import Dexie from 'dexie';
const db = new Dexie('fieldsets');
db.version(1).stores({
         fieldsets:'++id, user',

});

导出默认数据库;

import * as global from '../../global';
import getFields from './field-defs';
import db from '../../dexie_DB'

let g_fieldsets = [];

async function readFieldsets(fsetKey){
fsetKey = fsetKey+'_offers'
let res;
const userSets = await db.fieldsets.get(fsetKey);
if(!userSets || userSets.v !== 
process.env.REACT_APP_OFFERS_FIELDSET_VERSION){
    const defaultSets = [{
        name: 'Все поля',
        favourite: true,
        data:getFields().map((it,i)=>({
                name:it.name,
                hidden: false,}))
    },];
    res = defaultSets
    console.log(fsetKey,defaultSets)
    console.log('1')
    console.log(db)
    db.fieldsets.put({
        user: fsetKey,
        v:process.env.REACT_APP_OFFERS_FIELDSET_VERSION,
        data: defaultSets
    })
}else{
    res = userSets.data
}
g_fieldsets = res.map(it=>({
    ...it,
    data:it.data.map(item=>({
        ...item,
        def: getFields().find(def=> item.name===def.name)
    }))
}))
global.events.fieldsetsUpdated.next(null)
}

global.currentUser.subscribe(u=>{
if(u){
    readFieldsets(u.login)
}else{
    g_fieldsets = []
}
})

export function getFieldsets(force){
if(force || !g_fieldsets.length){
    if(!global.currentUser.getValue()){
        return [];
    }
    let _ = (readFieldsets(global.currentUser.getValue().login))
}
return g_fieldsets;
}

export function validate(name, it){
return {
    valid: null,
    msg: null
};
}

export function isEntityValid(it){
if(!it){
    return false
}if(!it.supplierId || !it.supplierId.length){
    return false
}if(!it.amount || it.amount<0){
    return false
}if(!it.offeredPrice || it.offeredPrice<0){
    return false
}
return true;
}

也许有一些驱动程序、一些程序、一些东西会破坏良好的工作流程

【问题讨论】:

    标签: javascript node.js reactjs indexeddb dexie


    【解决方案1】:

    我猜你已经安装了一个具有相同名称和表但没有自动递增主键的数据库。

    例如,您首先执行了这样的代码:

    import Dexie from 'dexie';
    const db = new Dexie('fieldsets');
    db.version(1).stores({
             fieldsets:'id, user',
    });
    

    然后,您将 id 更改为 ++id。

    尝试在浏览器中删除数据库并重新打开它。可以在 devtools 中完成,也可以通过 Dexie.delete('fieldsets').then(...) 完成。

    【讨论】:

      猜你喜欢
      • 2015-02-19
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多