I found a good way to get the user list out of mongodb. In case anyone need it:
async function doMongoDb() { const { MongoClient } = require("mongodb"); const username = encodeURIComponent("admin"); const password = encodeURIComponent("mypassword"); const clusterUrl = "localhost:27017"; const authMechanism = "DEFAULT"; const uri = `mongodb://${username}:${password}@${clusterUrl}/?authMechanism=${authMechanism}`; const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); try { await client.connect(); const database = client.db("nodebb"); const objects = database.collection("objects"); // get all user const query = { _key: /^user:\d+$/ }; // select fields we want to see const options = { projection: { _id: 0, username: true, _id: false, email: true, "email:confirmed": true, joindate: true, fullname: true }, }; const cursor = objects.find(query, options); if ((await cursor.count()) === 0) { console.log("No user found!"); } // iterate over the users await cursor.forEach(user => { if (user["email:confirmed"] == 1 ) { console.dir(user); } }); } finally { // make sure we close the connection await client.close(); } } // --------------------------------------------------- function init() { try { doMongoDb(); } catch(e) { console.log(e); } } // --------------------------------------------------- init();User defined navigation return 404 after press `F5`
-
I defined a navigation in admin console named
MyView
with route/myview
Bellowed is the init code in my plugin
plugin.http = {}; plugin.http.get = function(req, res, next) { res.render('myview', {}); }; plugin.init = function(data, callback) { data.router.get('/api/myview', plugin.http.get); callback(); };
code in
plugin.json
"library": "./library.js", "hooks": [ { "hook": "static:app.load", "method": "init" } ], "staticDirs": { "static": "./static" }, "less": [ "static/myview.min.less" ], "templates": "templates"
the
templates/myview.tpl
is quite simple with static HTML 'Hello nodebb'
Works well when access the page from navigation bar. But the title is missing, it's still 'Home'
Stay here, then press 'F5' it becames 404.
Any outstanding I should do Or I did it in a wrong way?
-
You have to define a
/api/myroute
and/myroute
so F5 works as well. You can use the helper class from routes.Check this out https://github.com/NodeBB/NodeBB/blob/master/src/routes/helpers.js#L5
Usage
var helpers = require.main.require('./src/routes/helpers'); plugin.init = function(data, callback) { helpers.setupPageRoute(data.router, '/myview', data.middleware, [], plugin.http.get); callback(); };
-
@baris Chrees! This really helps.