【问题标题】:500 Internal Server Error in simple Mean application简单平均应用程序中的 500 内部服务器错误
【发布时间】:2016-04-07 12:24:03
【问题描述】:

我正在 Thinkster.io 上做一个简单的 MEAN 堆栈教程,其中涉及创建一个简单的新闻应用程序。当我尝试将新项目发布到服务器时,我收到此错误POST http://localhost:3000/posts 500 (Internal Server Error)。它非常模糊,我不知道如何调试它。在chrome中我只能将错误追溯到这个方法。

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

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

但问题显然出在后端。即使您不愿意筛选代码,请概述我如何自己调试它。这是我的仓库的链接。 Link to Github

Chrome 输出

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 输出

【问题讨论】:

  • 当错误是服务器端时,显示客户端代码是没有意义的。
  • 500 (Internal Server Error) 表示服务器端脚本有问题
  • 是的,我意识到了这一点。只是觉得它可能会向人们展示我正在做的事情的要点。 @菲尔
  • 您的服务器的控制台输出是什么样的?
  • 这看起来像是来自 Chrome 的日志。实际服务器上的输出是什么样的?它应该在你启动 NodeJS 的控制台中写一些东西

标签: javascript angularjs node.js mongodb mean-stack


【解决方案1】:

Post 模型上的 cmets 字段需要一个评论 ID 数组。您正在尝试在 create Post 路由中创建 cmets。您应该按原样保存帖子,然后在事后添加 cmets。或者先创建 cmets 并将一个 Id 数组传递给 cmets 字段。

【讨论】:

    【解决方案2】:
    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);
    

    我必须添加/更新 Comments.js 文件。

    【讨论】:

      【解决方案3】:

      user.js

      如果您使用此代码进行注册

      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;
      

      *和角度文件 auth-service.ts *

      所以在注册的时候,当你用同一个邮箱注册的时候就会出现这个错误 好的

         import { Injectable } from "@angular/core";
      import { HttpClient } from "@angular/common/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);
              })
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-10-02
        • 2015-08-05
        • 1970-01-01
        • 2015-11-07
        • 1970-01-01
        • 2010-11-30
        • 1970-01-01
        • 2019-05-28
        • 2017-08-15
        相关资源
        最近更新 更多