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
3 Answers
Reset to default 3The 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
版权声明:本文标题:javascript - 500 Internal Server Error in simple Mean application - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742246905a2440022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论