您好我正在使用平均堆栈构建应用程序,使用角度控制器成功加载初始数据。但是,当添加用户时,它会尝试将用户对象发布到服务器端,但是“/ 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");
...
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);
response.status(500).send(err);
} else {
console.log("success");
response.status(200).end();
}
});
}
};
有人可以启发我出错的地方吗?
答案 0 :(得分:2)
看起来您没有向客户报告成功
postcontactlist : function(request, response){
Contact.insert(request.body, function(err, docs){
if(err){
console.log("error: " + err.message);
---> response.status(500).send(err)
} else {
console.log("success");
---> response.status(200).end()
}
});
}
答案 1 :(得分:1)
[我发布了另一个答案,因为问题作者已经重写了他们的问题 - @learningAngularIsFrustrating:你应该写一个新问题,而不是对你原来的问题进行如此大的编辑]
第2行和第34行的代码是什么? ((/Users/Sites/express/api/contacts.js:22:11)?我想这是insert
并认为你可能会更好地使用save
(那是什么?我使用的情况类似于你的情况。)
var contact = new Contact({ "name" : "Vincent Moyet", "email" : "vincentmoyet@gmail.com", "number" : "0987654321" })
contact.save(function(err, docs){...});