TypeError:Model.find不是函数

时间:2018-08-02 21:12:26

标签: node.js mongodb express

我对mongo / express很陌生。我只是试图在浏览器中导航到localhost:4000 / users /,以确保其正常工作,但我一直收到此错误。我一直在搜索,发现了关于同一问题的其他一些帖子,但是这些帖子的答案并没有运气。

TypeError: User.find is not a function
at /Users/joekleckler/Development/2018/projects/sanpit/server/routes/users.routes.js:27:10
at Layer.handle [as handle_request] (/Users/joekleckler/Development/2018/projects/sanpit/server/routes/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/joekleckler/Development/2018/projects/sanpit/server/routes/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/joekleckler/Development/2018/projects/sanpit/server/routes/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/joekleckler/Development/2018/projects/sanpit/server/routes/node_modules/express/lib/router/layer.js:95:5)
at /Users/joekleckler/Development/2018/projects/sanpit/server/routes/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/joekleckler/Development/2018/projects/sanpit/server/routes/node_modules/express/lib/router/index.js:335:12)
at next (/Users/joekleckler/Development/2018/projects/sanpit/server/routes/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/joekleckler/Development/2018/projects/sanpit/server/routes/node_modules/express/lib/router/index.js:174:3)
at router (/Users/joekleckler/Development/2018/projects/sanpit/server/routes/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/joekleckler/Development/2018/projects/sanpit/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/joekleckler/Development/2018/projects/sanpit/node_modules/express/lib/router/index.js:317:13)
at /Users/joekleckler/Development/2018/projects/sanpit/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/joekleckler/Development/2018/projects/sanpit/node_modules/express/lib/router/index.js:335:12)
at next (/Users/joekleckler/Development/2018/projects/sanpit/node_modules/express/lib/router/index.js:275:10)
at cors (/Users/joekleckler/Development/2018/projects/sanpit/node_modules/cors/lib/index.js:188:7)
at /Users/joekleckler/Development/2018/projects/sanpit/node_modules/cors/lib/index.js:224:17
at originCallback (/Users/joekleckler/Development/2018/projects/sanpit/node_modules/cors/lib/index.js:214:15)
at /Users/joekleckler/Development/2018/projects/sanpit/node_modules/cors/lib/index.js:219:13
at optionsCallback (/Users/joekleckler/Development/2018/projects/sanpit/node_modules/cors/lib/index.js:199:9)
at corsMiddleware (/Users/joekleckler/Development/2018/projects/sanpit/node_modules/cors/lib/index.js:204:7)
at Layer.handle [as handle_request] (/Users/joekleckler/Development/2018/projects/sanpit/node_modules/express/lib/router/layer.js:95:5)

models / user.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const UserSchema = new Schema({
    username: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    token: String
}, {collection : 'users'});

const User = mongoose.model('User', UserSchema);

module.exports = User;

user.routes.js

const express = require('express');
const app = express();
const UserRoute = express.Router();
var mongoose = require('mongoose');

// Require User model in our routes module
var User = require('./models/user');

UserRoute.route('/').get(function (req, res) {
    console.log('finding users');
    User.find({}, function (err, users){
    if(err){
      console.log(err);
    }
    else {
      res.json(users);
    }
  });
});

server.js

// server.js

const express = require('express'),
    path = require('path'),
    bodyParser = require('body-parser'),
    cors = require('cors'),
    mongoose = require('mongoose'),
    config = require('./config/DB');

mongoose.Promise = global.Promise;
mongoose.connect(config.DB).then(
    () => { console.log('Database is connected') },
    err => { console.log('Can not connect to the database' + err) }
);

var UserRoute = require('./server/routes/users.routes');
const app = express();
app.use(bodyParser.json());
app.use(cors());
app.use('/users', UserRoute);
const port = process.env.PORT || 4000;

const server = app.listen(port, function () {
    console.log('Listening on port ' + port);
});

Db.js

module.exports = {
    DB: 'mongodb://localhost:27017/sanpit'
};

再次,我对此很陌生,我做错了什么吗?请帮助

console.log(用户)

{ [Function: model]
  hooks: 
   Kareem {
     _pres: Map { 'save' => [Array], 'remove' => [Array] },
     _posts: Map { 'init' => [Array], 'save' => [Array] } },
  base: 
   Mongoose {
     connections: [ [Object] ],
     models: { User: [Circular] },
     modelSchemas: { User: [Object] },
     options: { pluralization: true },
     _pluralize: [Function: pluralize],
     plugins: [ [Array], [Array], [Array], [Array] ] },
  modelName: 'User',
  model: [Function: model],
  db: 
   NativeConnection {
     base: 
      Mongoose {
        connections: [Array],
        models: [Object],
        modelSchemas: [Object],
        options: [Object],
        _pluralize: [Function: pluralize],
        plugins: [Array] },
     collections: { users: [Object] },
     models: { User: [Circular] },
     config: { autoIndex: true },
     replica: false,
     options: null,
     otherDbs: [],
     relatedDbs: {},
     states: 
      { '0': 'disconnected',
        '1': 'connected',
        '2': 'connecting',
        '3': 'disconnecting',
        '99': 'uninitialized',
        disconnected: 0,
        connected: 1,
        connecting: 2,
        disconnecting: 3,
        uninitialized: 99 },
     _readyState: 0,
     _closeCalled: false,
     _hasOpened: false,
     _listening: false },
  discriminators: undefined,
  '$appliedMethods': true,
  '$appliedHooks': true,
  schema: 
   Schema {
     obj: 
      { username: [Object],
        password: [Object],
        token: [Function: String] },
     paths: 
      { username: [Object],
        password: [Object],
        token: [Object],
        _id: [Object],
        __v: [Object] },
     aliases: {},
     subpaths: {},
     virtuals: { id: [Object] },
     singleNestedPaths: {},
     nested: {},
     inherits: {},
     callQueue: [],
     _indexes: [],
     methods: {},
     methodOptions: {},
     statics: {},
     tree: 
      { username: [Object],
        password: [Object],
        token: [Function: String],
        _id: [Object],
        __v: [Function: Number],
        id: [Object] },
     query: {},
     childSchemas: [],
     plugins: [ [Object], [Object], [Object], [Object], [Object] ],
     s: { hooks: [Object] },
     _userProvidedOptions: { collection: 'users' },
     options: 
      { collection: 'users',
        typeKey: 'type',
        id: true,
        noVirtualId: false,
        _id: true,
        noId: false,
        validateBeforeSave: true,
        read: null,
        shardKey: null,
        autoIndex: null,
        minimize: true,
        discriminatorKey: '__t',
        versionKey: '__v',
        capped: false,
        bufferCommands: true,
        strict: true,
        pluralization: true },
     '$globalPluginsApplied': true },
  collection: 
   NativeCollection {
     collection: null,
     opts: 
      { bufferCommands: true,
        capped: false,
        '$wasForceClosed': undefined },
     name: 'users',
     collectionName: 'users',
     conn: 
      NativeConnection {
        base: [Object],
        collections: [Object],
        models: [Object],
        config: [Object],
        replica: false,
        options: null,
        otherDbs: [],
        relatedDbs: {},
        states: [Object],
        _readyState: 0,
        _closeCalled: false,
        _hasOpened: false,
        _listening: false },
     queue: [ [Array], [Array] ],
     buffer: true,
     emitter: 
      EventEmitter {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined } },
  Query: 
   { [Function]
     base: 
      Query {
        toConstructor: [Function: toConstructor],
        setOptions: [Function],
        collection: [Function: collection],
        '$where': [Function],
        where: [Function],
        equals: [Function: equals],
        eq: [Function: eq],
        or: [Function: or],
        nor: [Function: nor],
        and: [Function: and],
        gt: [Function],
        gte: [Function],
        lt: [Function],
        lte: [Function],
        ne: [Function],
        in: [Function],
        nin: [Function],
        all: [Function],
        regex: [Function],
        size: [Function],
        maxDistance: [Function],
        minDistance: [Function],
        mod: [Function],
        exists: [Function],
        elemMatch: [Function],
        within: [Function: within],
        box: [Function],
        polygon: [Function],
        circle: [Function],
        near: [Function: near],
        intersects: [Function: intersects],
        geometry: [Function: geometry],
        select: [Function: select],
        slice: [Function],
        sort: [Function],
        limit: [Function],
        skip: [Function],
        maxScan: [Function],
        batchSize: [Function],
        comment: [Function],
        maxTime: [Function],
        snapshot: [Function],
        hint: [Function],
        slaveOk: [Function],
        read: [Function],
        tailable: [Function],
        merge: [Function],
        find: [Function],
        cursor: [Function: cursor],
        findOne: [Function],
        count: [Function],
        distinct: [Function],
        update: [Function: update],
        updateMany: [Function: updateMany],
        updateOne: [Function: updateOne],
        replaceOne: [Function: replaceOne],
        remove: [Function],
        deleteOne: [Function],
        deleteMany: [Function],
        findOneAndUpdate: [Function],
        findOneAndRemove: [Function],
        _findAndModify: [Function],
        _wrapCallback: [Function],
        setTraceFunction: [Function],
        exec: [Function: exec],
        thunk: [Function],
        then: [Function],
        stream: [Function],
        selected: [Function: selected],
        selectedInclusively: [Function: selectedInclusively],
        selectedExclusively: [Function: selectedExclusively],
        _mergeUpdate: [Function],
        _optionsForExec: [Function],
        _fieldsForExec: [Function],
        _updateForExec: [Function],
        _ensurePath: [Function],
        _validate: [Function] } },
  '$__insertMany': [Function],
  '$init': Promise { <pending>, catch: [Function] } }

0 个答案:

没有答案