搜尋

首頁  >  問答  >  主體

node.js - mongoose使用过程中遇到的MissingSchemaError问题。

我就不多说了,直接上代码吧!
这是user文件:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;


var userSchema = new Schema({
        uid: {
            type: Number,
            unique: true
        },
        name: {
            type: String,
            required: true
        },
        phone: {
            type: String,
            required: true
        },
        password: {
            type: String,
            required: true
        }

    }
);


module.exports = mongoose.model('User', userSchema);

这是user的controller文件:



    var mongoose = require('mongoose'),
    User = mongoose.model('User'),
    _ = require('lodash');


/*增加一个user数据*/

exports.create = function (req, res, next) {
    var data = req.body;

    console.log(req + '%%%%%%%%%req content');

    console.log(data + '@@@@@@@@data');

    User.save(data, function (err) {
        console.log(err);
    }, function (res) {
        console.log(res);
    });

    return next()
};

这是router文件:

 
    var user = require('./controllers/user');


module.exports = function (app) {

    app.post('api/user/:id',user.create());

};

这是server文件:

  
    var express = require('express'),
    //session = require('express-session'),
    mongoose = require('mongoose'),
    router = require('./lib/router'),
    app = express();

app.use(express.static(__dirname + '/'));

mongoose.connect('mongodb://localhost/test');

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));


router(app);

console.log('you have  logged in  successfully!');

app.listen(4000);

  然后是报错的代码:

  throw new mongoose.Error.MissingSchemaError(name);
      ^
MissingSchemaError: Schema hasn't been registered for model "User".
Use mongoose.model(name, schema)
    at Mongoose.model (e:\自己做的项目\TradingPlatform\node_modules\mongoose\lib\index.js:350:13)
    at Object.<anonymous> (e:\自己做的项目\TradingPlatform\lib\controllers\user.js:9:21)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (e:\自己做的项目\TradingPlatform\lib\router.js:7:12)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (e:\自己做的项目\TradingPlatform\server.js:10:14)

Process finished with exit code 1

求各位大神解答

大家讲道理大家讲道理2782 天前780

全部回覆(1)我來回復

  • 迷茫

    迷茫2017-04-17 15:18:56

    controllers/user
    貼上這個檔案的程式碼吧

    回覆
    0
  • 取消回覆