简体   繁体   中英

Posting data from express node to mongodb using angularjs

Hi I am building an app using the mean stack, with an angular controller which loads the initial data successfully. However when a user is added it attempts to post the user object to the server side, however "/api/contacts" returns an error 500, (TypeError: undefined is not a function) . Code is below:

//complete error message :

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 routes

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();
        }
    });
}

    };

Can someone enlighten where im going wrong??

Looks like you are not reporting the success back to the client

   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()
                   }
        });

    }

[I'm posting another answer as the question author has rewritten their question - @learningAngularIsFrustrating: you should really have written a new question rather than make such large edits to your original one]

What is the code on line 2" ((/Users/Sites/express/api/contacts.js:22:11)? I guess it is the insert and think you may be better off with save (that's what I use in a similar situation to yours).

var contact = new Contact({ "name" : "Vincent Moyet", "email" : "vincentmoyet@gmail.com", "number" : "0987654321" })
contact.save(function(err, docs){...});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM