0

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" : "[email protected]", "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??

2 Answers 2

2

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

    }
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Simon H, I have updated my code with your suggestion, and added a screenshot of what is being posted. Im even now directly inserting the object . However still returning a 500 error status.
What message are you getting in the node consle. I don't think this is an angular issue but that your server is not working for some reason and posting pictures from the client side will not help
Hi Simon H, ive updated the main post, now includes the full error stack. Had a look at github.com/molnarg/node-http2/issues/100 . However, still unable to identify the source of the error.
1

[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" : "[email protected]", "number" : "0987654321" })
contact.save(function(err, docs){...});

5 Comments

Hi Simon H, apologies Im still relatively new to Stackflow. I have tried ".save" instead of ".insert", however receive the same exact error message.
OK. But can you check which function it is not finding by looking at the line numbers in the error message
I've done just that, however cannot identify the reason why its not working unfortunately, besides Im still a novice in node express.
have a go with my new suggestion above
Thanks a lot that worked!!! However with my code I found I can use the static "create" method. Its great as I now understand both methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.