TypeError: db.collection is not a function, CANNOT GET
incorect syntax, you must read property of db.collection, but you call that. Example:
db.collection['products']!!!db.collection['text'].save({ title: title, author: author, text: text }, callback);};module.exports.findBookByTitle = function (db, title, callback) { db.collection['text'].findOne({ title: title }, function (err, doc) { if (err || !doc) callback(null); else callback(doc.text); });};module.exports.findProductsByName = function (db, name, callback) { db.collection['products'].findOne({
For example
var object = { 'some_value': 'value', 'some_methid': function(){ return 'method result'}}
You can read and set property 'some_value', for example:
object['some_value'] // return 'value'object.some_value // return 'value'
// STEP 2
Ok, in your methods of database.js you pass db variable, but this is not of db instance, it is mongoose model, and you must write like this:
module.exports.findBookByTitle = function (model, title, callback) { model.findOne({ title: title }, function (err, doc) { if (err || !doc) callback(null); else callback(doc.text); });};module.exports.findProductsByName = function (model, name, callback) { model.findOne({ name: name }, function (err, doc) { if (err || !doc) callback(null); else callback(doc.products); });};
You can only declare module.exports once in a file, so you should change:
In your database.js file:
module.exports.saveBook
to
exports.saveBook
and so on
Take a look at this: https://www.sitepoint.com/understanding-module-exports-exports-node-js/