I’m getting this error in my addon when adding a new note. I can’t figure out what the problem is. What are the possible ways that this message can occur?
There seems to be just one place where the message “no such note” occurs in the source, render.rs but that doesn’t seem right, the error isn’t occurring when rendering a card.
impl Collection {
/// Render an existing card saved in the database.
pub fn render_existing_card(
&mut self,
cid: CardId,
browser: bool,
partial_render: bool,
) -> Result<RenderCardOutput> {
let card = self.storage.get_card(cid)?.or_invalid("no such card")?;
let note = self
.storage
.get_note(card.note_id)?
.or_invalid("no such note")?; <----
The note_will_be_added hook is triggered as part of mw.col.add_note(...).
Could the message be from an addon that uses that hook and (incorrectly) expects the Note ID to be set at that point?
Could be, the note adding is triggering additional hooks from my other addon but I fixed the that exact problem there previously so I’m perplexed as to what it is this time. The weird thing is that the error only occurs occasionally.
I eventually found the problem. It was a problem with my other addon during the note_will_be_added hook. The cause was two-fold:
I was calling mw.col.update_notes() on a new note not yet added
I was duplicating a new note using my code from here which called _load_from_backend_note which also throws the error when id=0
I fixed the first by correctly omitting new notes from being passed to mw.col.update_notes and the second by changing my note duplicate function to this:
def duplicate_note(note: Note) -> Note:
"""
Duplicate a note with deepcopy, copying everything except the col attribute
as that needs to identical to the original note
"""
new_note = type(note).__new__(type(note))
for k, v in note.__dict__.items():
if k == "col":
setattr(new_note, k, note.col)
else:
setattr(new_note, k, deepcopy(v))
return new_note