admin管理员组

文章数量:1327988

I am doing a simple MEAN stack tutorial on Thinkster.io which involves creating a simple news app. When I try to post a new item to the server I get this error, POST http://localhost:3000/posts 500 (Internal Server Error). It is very vague and I have no idea how to debug it. In chrome I can only trace the error to this method.

//method in service
o.create = function(post) {
        return $http.post('/posts', post).success(function(data) {
            o.posts.push(data);
    });
};  

and

//usage
$scope.addPost = function(){
        if(!$scope.title || $scope.title === ""){
            return;
        }
        posts.create({
            title: $scope.title,
            link: $scope.link,
            upvotes : 0,
            ments: [
            {author: "Joe", body: "Class!!", upvotes: 0},
            {author: "Coco", body: "Woof Woof!!", upvotes: 0}
            ]
            })
        $scope.title = "";
        $scope.link = "";
};   

But the problem is obviously in the backend. Even if you are unwilling to sift through the code please outline how I can debug this for myself. Here is a link to my repo. Link to Github

Chrome outputs

XHR finished loading: GET "http://localhost:3000/posts". angular.js:9818
POST http://localhost:3000/posts 500 (Internal Server Error) angular.js:9818
XHR finished loading: POST "http://localhost:3000/posts". angular.js:9818

CMD Outputs

I am doing a simple MEAN stack tutorial on Thinkster.io which involves creating a simple news app. When I try to post a new item to the server I get this error, POST http://localhost:3000/posts 500 (Internal Server Error). It is very vague and I have no idea how to debug it. In chrome I can only trace the error to this method.

//method in service
o.create = function(post) {
        return $http.post('/posts', post).success(function(data) {
            o.posts.push(data);
    });
};  

and

//usage
$scope.addPost = function(){
        if(!$scope.title || $scope.title === ""){
            return;
        }
        posts.create({
            title: $scope.title,
            link: $scope.link,
            upvotes : 0,
            ments: [
            {author: "Joe", body: "Class!!", upvotes: 0},
            {author: "Coco", body: "Woof Woof!!", upvotes: 0}
            ]
            })
        $scope.title = "";
        $scope.link = "";
};   

But the problem is obviously in the backend. Even if you are unwilling to sift through the code please outline how I can debug this for myself. Here is a link to my repo. Link to Github

Chrome outputs

XHR finished loading: GET "http://localhost:3000/posts". angular.js:9818
POST http://localhost:3000/posts 500 (Internal Server Error) angular.js:9818
XHR finished loading: POST "http://localhost:3000/posts". angular.js:9818

CMD Outputs

Share Improve this question edited Jan 4, 2016 at 0:07 Greevman asked Jan 3, 2016 at 21:33 GreevmanGreevman 661 gold badge4 silver badges11 bronze badges 7
  • There's no point in showing client-side code when the error is server-side. – Phil Commented Jan 3, 2016 at 21:35
  • 3 500 (Internal Server Error) means problem in server side script – Scarecrow Commented Jan 3, 2016 at 21:35
  • Yeah I realise this. Just felt it might show people the gist of what I am doing. @Phil – Greevman Commented Jan 3, 2016 at 21:44
  • What does the console output look like from your server? – chedabob Commented Jan 3, 2016 at 21:52
  • 1 That looks like the logs from Chrome. What does the output look like on the actual server? It should be writing something in the console where you launched NodeJS – chedabob Commented Jan 3, 2016 at 22:03
 |  Show 2 more ments

3 Answers 3

Reset to default 3

The ments field on the Post model is expecting an array of ment ids. You are trying to create ments inside the create Post route. You should save the post as is and then add the ments after the fact. Or create ments first and pass an array of Ids to the ments field.

var mongoose = require('mongoose');

var CommentSchema = new mongoose.Schema({
  body: String,
  author: String,
  upvotes: {type: Number, default: 0}
});

CommentSchema.methods.upvote = function(cb) {
    this.upvotes += 1;
    this.save(cb);
}

mongoose.model('Comment', CommentSchema);

I had to add/update the Comments.js file.

user.js

If you are using this code for signup

const express = require("express");
const bcrypt = require('bcrypt')

const router = express.Router();
const User = require("../models/user")

router.post("/register", (req, res, next) =>{
    bcrypt.hash(req.body.password, 10)
    .then(hash =>{
        const user = new User({
            email: req.body.email,
            password: hash
        });
        user.save()
        .then(result => {
            res.status(201).json({
                message: "User created!",
                result: result
            })
        })
        .catch(err =>{
            res.status(500).json({
                err:err 
            })
        })
    })
});
module.exports = router;

*and angular file auth-service.ts *

So while signing up, this error occurs when you signup with the same mail ok

   import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/mon/http";
import { AuthData } from "./auth-data.model";

@Injectable({providedIn: "root"})

export class AuthService{
    constructor(private http: HttpClient ) {}

    createUser(email: string, password: string ){
        const authData: AuthData =  { email: email, password: password };
        this.http.post("http://localhost:3000/api/user/register", authData)
        .subscribe(Response =>{
            console.log(Response);
        })
    }
}

本文标签: javascript500 Internal Server Error in simple Mean applicationStack Overflow