Skip to content

bug: storeChat stores 'undefined' string instead of NULL for last_message_time #14

Description

@theflysurfer

Problem

storeChat() in database.ts converts undefined timestamps to the string "undefined" instead of NULL:

last_message_time:
  chat.last_message_time instanceof Date
    ? chat.last_message_time.toISOString()
    : chat.last_message_time === null
      ? null
      : String(chat.last_message_time),  // ← String(undefined) === "undefined"

In JavaScript, String(undefined) returns the string "undefined", which gets stored in SQLite as a text value.

Impact

  • list_chats sorted by last_active is completely broken — SQLite sorts "undefined" > ISO dates alphabetically
  • In my DB: 386/866 chats had last_message_time = 'undefined'
  • These chats sort to the top, hiding actually active conversations

Fix

One-line change in storeChat():

const lmt = chat.last_message_time;
stmt.run({
  jid: chat.jid,
  name: chat.name ?? null,
  last_message_time:
    lmt instanceof Date
      ? lmt.toISOString()
      : (lmt == null || String(lmt) === "undefined")
        ? null
        : String(lmt),
});

Plus a one-time cleanup on init:

UPDATE chats SET last_message_time = NULL WHERE last_message_time IN ('undefined', 'null', '');

Happy to submit a PR if you'd like.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions