Help with my Custom Function
-
@baris I can see my console log being called in library.init.
I don't remember 'activating' our custom function in v2 before. And I don't see any error messages from our custom function in the log file.
These hooks haven't change right?
action:topic.reply
action:topic.post -
@baris yup, I did restart a few times, but the function is still not triggering.
EDIT:
It's triggering now. I added a new user to the group, and when I create a new post using the new user, it triggered my function. I guess the function does not trigger for old posts?
-
Note that hooks are generally for reacting to (or modifying) events.
Some may happen during startup, but usually the function from a hook is triggered at some point during some request to NodeBB.If you want to run something on existing content after installing a plugin you might want to look into upgrades - functions that run on startup only once, saved into database afterwards to avoid re-running them. See for example the dbsearch plugin changing the MongoDB schema: https://github.com/barisusakli/nodebb-plugin-dbsearch/ (tl;dr you just need a module that exports what's needed to
plugin.json
in theupgrades
array. You can see the format in theupgrades/dbsearch_change_mongodb_schema.js
.).You can obviously also try doing something similar yourself in e.g.
static:app.load
, but why bother when NodeBB already has a mechanism for it -
@oplik0 yeah and if you don't want an upgrade script and just want to run some code that connects to the database to modify stuff or create reports etc. you can run custom script. These go in the nodebb folder and can be executed by
node my_custom_script.js
. Below is a template that we use frequently./* globals require, console, process */ 'use strict'; const nconf = require('nconf'); nconf.file({ file: 'config.json', }); nconf.defaults({ base_dir: __dirname, views_dir: './build/public/templates', upload_path: 'public/uploads', }); const db = require('./src/database'); db.init(async (err) => { if (err) { console.log(`NodeBB could not connect to your database. Error: ${err.message}`); process.exit(); } await doSomethingUseful(); console.log('done'); process.exit(); }); async function doSomethingUseful() { console.log('global object', await db.getObject('global')); }
-