Update currently active note property using MetaEdit api

This is an extension of my other post. The following code exactly as intended except for the Update new file properties part.

I have a button in the master journal log file that creates a new journal entry. The master journal also keeps track of the current most latest (old) journal entry in YAML.
After creating a new entry, that entry’s ‘Previous Entry’ property (written as Previous Entry:: ) should be updated with a link to the old latest entry… But that doesn’t happen, despite the opposite (the last line of the code) working fine.

I don’t get any console errors either. Where am I mistaken?

<%*
// Initiate APIs
const meta = this.app.plugins.plugins["metaedit"].api;
const dv = app.plugins.plugins.dataview.api;

// Get path of master journal
let masterJPath = await dv.page("Journal Log").file.path;

// Get old file info
let oldFileName = await meta.getPropertyValue("latestEntryFileName", masterJPath);
let oldFileIndex = await meta.getPropertyValue("latestEntry", masterJPath);
let oldFilePath = await dv.page(`${oldFileName}`).file.path;

// Get time
let now = await tp.date.now('YYYY-MM-DD HH-mm-ss');

// Generate new entry info
let newFileName = await `J ${oldFileIndex+1} ${now}`;
let newFilePath = tp.file.path(true);

// Rename new entry
await tp.file.rename(newFileName);

// Update new file properties
await meta.update("Previous Entry", `[[${oldFileName}]]`, newFilePath);

// Update master journal properties
await meta.update("latestEntry", `${oldFileIndex+1}`, masterJPath);
await meta.update("latestEntryFileName", `${newFileName}`, masterJPath);

// Update old file properties
await meta.update("Next Entry", `[[${newFileName}]]`, oldFilePath);
-%>
1 Like

A quick guess: If you print newFilePath out to the console, is it correctly the path to your newly-renamed note? The path of the note changes when you rename it.

Ah, I see the problem now that you mention it. The way the system works is that when the button is clicked, a new note named New Note.md is created at the desired path, which is then renamed to the correct journal entry.

So essentially, the path is correct, but the name of the note is not. Because of the code being asynchronous, it tries to change the property of New Note instead of J 1 YYYY-MM-DD HH-mm

1 Like

I have replaced that line with

// Update new file properties
await meta.update("Previous Entry", `[[${oldFileName}]]`, tp.file.path(true));
console.log(tp.file.path(true));

but it still doesn’t work. I see the property being added for a split second and then it vanishes as soon as the code vanishes as well. Is there some kind of behaviour that I’m not aware of?

Oh that’s unexpected! What is left, a blank “Previous Entry” field?
Is the previous entry field below your code in the new file? I’m wondering if it is being overwritten ny the rest of the template processing.

Yup. I see the link to the previous note be added to the current note, but as soon as the entire code block vanishes after completing all the tasks, the ‘Previous Entry’ property is emptied out as well.

obsidian_error_gif

I thought so too, but that logic didn’t feel right based on the tutorials I’ve seen. I’m at a loss from this. :confused:

Will anything break if you move the code block to the bottom of your template file? (Or at least below where your previous entry field is?) I’m surprised the write to the field was wprking at all if the field didn;t exist yet in the file!

Nothing breaks. Just the same outcome occurs.

The same thing happens even if I move the properties up to the frontmatter.

Well that is confusing to me! I’m wondering if separating out the code to update the current file’s field into a second template and inserting it after the first one is done would help but I have no intuition about where the blanking out is coming from so it’s a wild guess. i’ll try to experiment a little also. I haven’t tried to use metaedit during Templater execution before.

1 Like

Same issue for me. MetaEdit “update” seems to not be working within a templater template

Like magic as soon as I post to the forum, I get it to work… “update” does not work with “await” in front of it.

From the original post:
await meta.update("Previous Entry", [[${oldFileName}]], newFilePath);

…may work as:
meta.update("Previous Entry", [[${oldFileName}]], newFilePath);

1 Like

Glad it worked!! I have absolutely no idea why that would work - if anyone has an idea or explanation I’d love to hear it! Yay for learning new things.