【问题标题】:Simple write to Dexie without knowing the schema在不知道架构的情况下简单地写入 Dexie
【发布时间】:2021-03-25 02:57:27
【问题描述】:

是否可以在不知道架构的情况下将整个对象写入 Dexie? 我只想这样做:

var db = new Dexie(gameDataLocalStorageName);
                db.version(1).stores({
                    myData: "gameData"
                });
                db.myData.put(gameData);
                console.log(db.myData.get('gameData'));

但我收到以下错误:

Unhandled rejection: DataError: Failed to execute 'put' on 'IDBObjectStore': Evaluating the object store's key path did not yield a value.
 DataError: Failed to execute 'put' on 'IDBObjectStore': Evaluating the object store's key path did not yield a value.

【问题讨论】:

    标签: dexie


    【解决方案1】:

    错误是因为您已指定架构以使用入站键“gameData”,即要求每个对象都将属性“gameData”作为其主键。

    如果您不需要在对象中包含主键,您可以将架构声明为{myData: ""} 而不是{myData: "gameData"}。通过这样做,您需要在调用db.myData.put() 时提供与对象分开的主键。

    请参阅 inbound vs non-inbound keysdetailed schema syntax 的文档

    var db = new Dexie(gameDataLocalStorageName);
    db.version(1).stores({
      myData: ""
    });
    Promise.resolve().then(async () => {
      await db.myData.put(gameData, 'gameData'); // 'gameData' is key.
      console.log(await db.myData.get('gameData'));
    }).catch(console.error);
    

    由于我们在这里更改了主键,因此您需要先在 devtools 中删除数据库,然后才能使用。

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多