【问题标题】:Fetch all records from WatermelonDB database从 WatermelonDB 数据库中获取所有记录
【发布时间】:2022-08-05 15:13:59
【问题描述】:

我想知道为什么下面的代码块返回整个数据库对象?

const getChatListFromDatabase = async () => {
    try {
      const chatList = await database.collections
        .get(\"chatList\")
        .query()
        .fetch();
      return chatList;
    } catch (e) {
      console.log(e);
    }
  };

  useEffect(() => {
    // prettier-ignore
    (
     async () => {
      const list = await getChatListFromDatabase();
      console.log(\"the list is : \", list)
      }
    )()
  });

聊天列表和列表包含数据库对象而不是所有记录。 我想知道我是否写错了查询或者我错过了其他东西。

以下是查询返回的对象:

the list is :  Array [
  ChatListModel {
    \"__changes\": BehaviorSubject {
      \"_value\": [Circular],
      \"closed\": false,
      \"hasError\": false,
      \"isStopped\": false,
      \"observers\": Array [],
      \"thrownError\": null,
    },
    \"_isEditing\": false,
    \"_preparedState\": null,
    \"_raw\": Object {
      \"_changed\": \"\",
      \"_status\": \"created\",
      \"email\": \"qwe@asd.com\",
      \"id\": \"j9lumqrj3cf5usat\",
      \"lastMessage\": \"bro this works\",
      \"messageStatus\": \"sent\",
      \"profileImageUrl\": \"https://i.pinimg.com/564x/86/6f/2e/866f2ec93c7c05560a76fbbbc470e161.jpg\",
      \"time\": \"12:34\",
      \"userId\": \"62041ac4cb15d7549f0e61f7\",
      \"username\": \"qwe\",
    },
    \"_subscribers\": Array [],
    \"collection\": Collection {
      \"_cache\": RecordCache {
        \"_debugCollection\": [Circular],
        \"map\": Map {
          \"j9lumqrj3cf5usat\" => [Circular],
        },
        \"recordInsantiator\": [Function anonymous],
        \"tableName\": \"chatList\",
      },
      \"_subscribers\": Array [],
      \"changes\": Subject {
        \"closed\": false,
        \"hasError\": false,
        \"isStopped\": false,
        \"observers\": Array [],
        \"thrownError\": null,
      },
      \"database\": Database {
        \"_isBeingReset\": false,
        \"_isBroken\": false,
        \"_resetCount\": 0,
        \"_subscribers\": Array [],
        \"_workQueue\": WorkQueue {
          \"_db\": [Circular],
          \"_queue\": Array [],
          \"_subActionIncoming\": false,
        },
        \"adapter\": DatabaseAdapterCompat {
          \"underlyingAdapter\": SQLiteAdapter {
            \"_dbName\": \"dbHello\",
            \"_dispatcher\": SqliteNativeModulesDispatcher {
              \"_tag\": 1,
            },
            \"_dispatcherType\": \"asynchronous\",
            \"_initPromise\": Promise {
              \"_U\": 0,
              \"_V\": 1,
              \"_W\": undefined,
              \"_X\": null,
            },
            \"_migrationEvents\": undefined,
            \"_tag\": 1,
            \"migrations\": undefined,
            \"schema\": Object {
              \"tables\": Object {
                \"chatList\": Object {
                  \"columnArray\": Array [
                    Object {
                      \"name\": \"userId\",
                      \"type\": \"string\",
                    },
                    Object {
                      \"name\": \"username\",
                      \"type\": \"string\",
                    },
                    Object {
                      \"name\": \"email\",
                      \"type\": \"string\",
                    },
                    Object {
                      \"name\": \"lastMessage\",
                      \"type\": \"string\",
                    },
                    Object {
                      \"name\": \"time\",
                      \"type\": \"string\",
                    },
                    Object {
                      \"name\": \"messageStatus\",
                      \"type\": \"string\",
                    },
                    Object {
                      \"name\": \"profileImageUrl\",
                      \"type\": \"string\",
                    },
                  ],
                  \"columns\": Object {
                    \"email\": Object {
                      \"name\": \"email\",
                      \"type\": \"string\",
                    },
                    \"lastMessage\": Object {
                      \"name\": \"lastMessage\",
                      \"type\": \"string\",
                    },
                    \"messageStatus\": Object {
                      \"name\": \"messageStatus\",
                      \"type\": \"string\",
                    },
                    \"profileImageUrl\": Object {
                      \"name\": \"profileImageUrl\",
                      \"type\": \"string\",
                    },
                    \"time\": Object {
                      \"name\": \"time\",
                      \"type\": \"string\",
                    },
                    \"userId\": Object {
                      \"name\": \"userId\",
                      \"type\": \"string\",
                    },
                    \"username\": Object {
                      \"name\": \"username\",
                      \"type\": \"string\",
                    },
                  },
                  \"name\": \"chatList\",
                  \"unsafeSql\": undefined,
                },
              },
              \"unsafeSql\": undefined,
              \"version\": 1,
            },
          },
        },
        \"collections\": CollectionMap {
          \"map\": Object {
            \"chatList\": [Circular],
          },
        },
        \"schema\": Object {
          \"tables\": Object {
            \"chatList\": Object {
              \"columnArray\": Array [
                Object {
                  \"name\": \"userId\",
                  \"type\": \"string\",
                },
                Object {
                  \"name\": \"username\",
                  \"type\": \"string\",
                },
                Object {
                  \"name\": \"email\",
                  \"type\": \"string\",
                },
                Object {
                  \"name\": \"lastMessage\",
                  \"type\": \"string\",
                },
                Object {
                  \"name\": \"time\",
                  \"type\": \"string\",
                },
                Object {
                  \"name\": \"messageStatus\",
                  \"type\": \"string\",
                },
                Object {
                  \"name\": \"profileImageUrl\",
                  \"type\": \"string\",
                },
              ],
              \"columns\": Object {
                \"email\": Object {
                  \"name\": \"email\",
                  \"type\": \"string\",
                },
                \"lastMessage\": Object {
                  \"name\": \"lastMessage\",
                  \"type\": \"string\",
                },
                \"messageStatus\": Object {
                  \"name\": \"messageStatus\",
                  \"type\": \"string\",
                },
                \"profileImageUrl\": Object {
                  \"name\": \"profileImageUrl\",
                  \"type\": \"string\",
                },
                \"time\": Object {
                  \"name\": \"time\",
                  \"type\": \"string\",
                },
                \"userId\": Object {
                  \"name\": \"userId\",
                  \"type\": \"string\",
                },
                \"username\": Object {
                  \"name\": \"username\",
                  \"type\": \"string\",
                },
              },
              \"name\": \"chatList\",
              \"unsafeSql\": undefined,
            },
          },
          \"unsafeSql\": undefined,
          \"version\": 1,
        },
      },
      \"modelClass\": [Function ChatListModel],
    },
  },
]
  • 你解决了吗?

标签: database react-native sqlite watermelondb


【解决方案1】:

尝试这样的事情。

try {
      const chatList = await database.collections
        .get("chatList")
        .query()
        .fetch();
      return chatList.ChatListModel.collection.database;

【讨论】:

  • 不工作,给出错误:“未定义不是一个对象(评估'_chatList.ChatListModel.collection')”
  • 这是西瓜数据库返回整个模型对象而不是字段数据的预期吗?
  • 试试看。不同的 Collectionname 等等。
【解决方案2】:

是的,预计会返回这样的数据。要么使用 observable,要么映射你的对象。例如:在你的数据库模型中创建一个方法

 getChat(): ChatType {
    return {
      chatId: this.chatId,
      value: this.value,
    };
  }

在您的代码中的地图之后,例如:

const list = await getChatListFromDatabase().map((item => item.getChat());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 2010-09-22
    • 1970-01-01
    • 2011-06-17
    相关资源
    最近更新 更多