TypeError:object不是节点Js中的函数

时间:2016-06-25 09:01:23

标签: javascript node.js mongodb rest

我是nodejs的初学者,我正在开发一个 REST api来与mongodb进行交互。我使用了express和mongoose,如下面的教程所述:

https://codeforgeek.com/2015/08/restful-api-node-mongodb/

我已经设置了mongo db,这是我在节点js中的服务器代码:

var express     =   require("express");
var app         =   express();
var bodyParser  =   require("body-parser");
var router      =   express.Router();
var mongoOp     =   require("./models/mongo");
//var user        =   require("./Entities/User");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({"extended" : false}));
router.get("/",function(req,res){
    res.json({"error" : false,"message" : "Hello World"});
});
router.route("/users")
    .get(function(req,res){
        var response = {};
        mongoOp.find({},function(err,data){
        // Mongo command to fetch all data from collection.
            if(err) {
                response = {"error" : true,"message" : "Error fetching data"};
            } else {
                response = {"error" : false,"message" : data};
            }
            res.json(response);
        });
    })
    .post(function(req,res){
        var db = new mongoOp();
        var response = {};
        // fetch email and password from REST request.
        // Add strict validation when you use this in Production.
        db.userEmail = req.body.email;
        // Hash the password using SHA1 algorithm.
        db.userPassword =  require('crypto')
                          .createHash('sha1')
                          .update(req.body.password)
                          .digest('base64');
        db.save(function(err){
        // save() will run insert() command of MongoDB.
        // it will add new data in collection.
            if(err) {
                response = {"error" : true,"message" : "Error adding data"};
            } else {
                response = {"error" : false,"message" : "Data added"};
            }
            res.json(response);
        });
    });
app.use('/',router);
app.listen(3000);
console.log("Listening to PORT 3000");

这是我的mongo模块

var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/Appdb");
//Create instance of Schema
var mongoSchema = mongoose.schema;
//Create Schema
var userSchema = {
    "userEmail":String,
    "userPassword":String
}
//Create model if not exists.
module.export = mongoose.model('userLogin',userSchema);

当我使用RESTClient(firefox中的插件来测试REStful应用程序)时,它会出现以下错误:

  

TypeError:object不是E:\ node \ server.js中的函数:30:18
  在Layer.handle [as handle_request]   (E:\ node \ node_modules \ express \ lib \ router \ layer.js:95:5).........

enter image description here

任何人都可以帮助我......

1 个答案:

答案 0 :(得分:1)

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

使用出口不出口