db.getSortedSetRange is not a function
Sorry to bother you again, this is probably something small, and likely my fault.
Here's the simple file I created:
var async = require('async');
var Data = require('../nodebb-plugin-import/server/data.js');
Data.eachPost(console.log,
function (err) {
if (err) {
throw err;
}
console.log("DONE!!");
}
);
When I run it I get this error:
/home/fidelix/www/anbient/forum/node_modules/nodebb-plugin-import/server/data.js:476
db.getSortedSetRange(setKey, start, end, function(err, ids) {
^
TypeError: db.getSortedSetRange is not a function
at /home/fidelix/www/anbient/forum/node_modules/nodebb-plugin-import/server/data.js:476:8
at Object.async.whilst (/home/fidelix/www/anbient/forum/node_modules/nodebb-plugin-import/node_modules/async/lib/async.js:675:13)
at Object.Data.processIdsSet (/home/fidelix/www/anbient/forum/node_modules/nodebb-plugin-import/server/data.js:468:9)
at Object.Data.processSet (/home/fidelix/www/anbient/forum/node_modules/nodebb-plugin-import/server/data.js:412:15)
at Object.Data.each (/home/fidelix/www/anbient/forum/node_modules/nodebb-plugin-import/server/data.js:388:15)
at Object.Data.eachPost (/home/fidelix/www/anbient/forum/node_modules/nodebb-plugin-import/server/data.js:290:15)
at Object.<anonymous> (/home/fidelix/www/anbient/forum/node_modules/nodebb-plugin-import-ipboard/ipb.js:4:6)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
Process finished with exit code 1
Any ideas why it's not defined? Looking at the db object it seems to have the proper configs (mongodb, localhost, etc)...
i think I know why, i will fix it tomorrow
hey, I found the issue, but it's NOT** (edit) an easy fix, the thing is that you're trying to use the database before it had some time to load - there is a callback to db.init() but the issue with this one is if you're already running NodeBB at the same-time, the callback would never execute. I have a fix in mind, but it's Easter! so I am give you a very stupid workaround till i fix this the right way, hopefully next week.
timeout way
var async = require('async');
var Data = require('../nodebb-plugin-import/server/data.js');
// just wrap it in a timeout till it's ready.
setTimeout(function() {
Data.eachPost(console.log,
function (err) {
if (err) {
throw err;
}
console.log("DONE!!");
}
);
}, 3000); // 3 seconds worked for me, you might need more depending on how large your db-index is.
interval way
var async = require('async');
var Data = require('../nodebb-plugin-import/server/data.js');
var go = function () {
Data.eachPost(console.log,
function (err) {
if (err) {
throw err;
}
console.log("DONE!!");
}
);
};
// you can test it every 500ms
var interval = setInterval(function() {
if (Data.db.client) {
clearInterval(interval);
go();
}
}, 500);
Both solutions are very stupid, but if you're writing a one-off script, they should work
Thank you so much @akhoury. This worked!
Ops... let me leave it open...