TypeError: db.collection is not a function, CANNOT GET

99封情书 提交于 2019-12-04 14:57:15

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/

in package.json folder find mongodb inside dependencies and update with these value:

"mongodb": "^2.2.33",

Then Run

npm install

again. That will solve your probem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!