插入或保存到mongodb会返回500服务器错误

时间:2015-10-07 13:14:52

标签: javascript node.js mongodb express mongoose

您好我正在尝试使用平均堆栈构建一个crud应用程序。在加载时,快速应用程序成功检索"联系人列表"从数据库中收集。但是,当尝试发布到" / api / contacts" ...时收到错误500(TypeError:undefined不是函数)。我已经漫游无数的博客/论坛寻找解决方案,但是现在我迷失了,但仍然指出错误背后的来源。欢迎所有建议。

以下是完整的错误堆栈......

TypeError: undefined is not a function
    at module.exports.postcontactlist (/Users/Sites/express/api/contacts.js:22:11)
    at callbacks (/Users/Sites/express/node_modules/express/lib/router/index.js:164:37)
    at param (/Users/Sites/express/node_modules/express/lib/router/index.js:138:11)
    at pass (/Users/Sites/express/node_modules/express/lib/router/index.js:145:5)
    at Router._dispatch (/Users/Sites/express/node_modules/express/lib/router/index.js:173:5)
    at Object.router (/Users/Sites/express/node_modules/express/lib/router/index.js:33:10)
    at next (/Users/express/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.methodOverride [as handle] (/Users/Sites/express/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:48:5)
    at next (/Users/Sites/express/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.urlencoded [as handle] (/Users/Sites/express/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js:46:27)
    at next (/Users/Sites/express/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at /Users/Sites/express/node_modules/express/node_modules/connect/lib/middleware/json.js:91:7
    at IncomingMessage.onEnd (/Users/Sites/express/node_modules/express/node_modules/connect/node_modules/raw-body/index.js:57:7)
    at IncomingMessage.g (events.js:199:16)
    at IncomingMessage.emit (events.js:104:17)
    at _stream_readable.js:908:16
POST /api/contacts 500 4ms - 1.69kb

这是我的代码:

// Server.js

var contactlist = require("./api/contacts");
....
//server
app.get("/api/contacts", contactlist.getcontactlist);
app.post("/api/contacts", contactlist.postcontactlist);

// API / contacts.js

var express = require("express");
var http = require("http");
var bp = require("body-parser");
var mongoose = require("mongoose");
var schema = require("../models/contactsdb"); 

var Contact = mongoose.model("Contact", schema.contactSchema, "contactlist");

module.exports = {

    getcontactlist : function (request, response) { 

        Contact.find({ }, function (err, docs) { 
            response.send(docs);
        });

    },
    postcontactlist : function(request, response){

        console.log("request: " + request.body);


        Contact.insert({ name : "Vincent Moyet", email : "vincentmoyet@gmail.com", number : "0987654321" }, function(err, docs){

            if(err){
                var errorMsg = "error: " + err.message;
                console.log(errorMsg + " / " + err.getLastError());
                response.status(500).send(err);

            } else {
                console.log("success");
                response.status(200).end();
            }

        });

    }

};

///模型/ contactsBb

var mongoose = require("mongoose"); 

mongoose.connect('mongodb://localhost:27017/contactlist');

exports.contactSchema = new mongoose.Schema({
    name: String,
    email: String,
    number: String
});

1 个答案:

答案 0 :(得分:2)

将您的功能从insert更改为create

Contact.create(contact, function(err, docs){    
    if(err){
        var errorMsg = "error: " + err.message;
        console.log(errorMsg + " / " + err.getLastError());
        response.status(500).send(err);    
    } else {
        console.log("success");
        response.status(200).end();
    }

});