2016-08-06 4 views
0

私のルートに問題があり、mongodbでデータを取得/保存しています。 JSONを保存したり、投稿しなかったりすると、検証エラーが発生しているようです。何か案は?平均スタック - GETとPOSTでクエリが実行されない、またはmongodbに保存されない

var app = angular.module('myApp', []); 

app.controller('MainCtrl', function($scope, $http) { 
    $http.get('/api/mongo') 
    .then(function(response) { 
     console.log(response.data); 
     $scope.myData = response.data; 
    }); 
}); 

app.controller('FormCtrl', function($scope, $http) { 
    $scope.formData = {}; 

    $scope.addSite = function() { 
     $http.post('/api/create', $scope.formData) 
      .success(function(data) { 
       console.log($scope.formData); 
       $scope.formData = {}; // clear the form so our user is ready to enter another 
       swal(
        'Good job!', 
        'Site was added!', 
        'success' 
       ); 
      }) 
      .error(function(data) { 
       console.log('Error: ' + data); 
      }); 
    }; 
}); 

私の急行路線

// grab the things we need 
var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

// create a schema 
var sitesEntrySchema = new Schema({ 
    ip: { 
     type: String, 
     required: true, 
     trim: true 
    }, 
    domain: { 
     type: String, 
     required: true, 
     trim: true 
    }, 
    wp: { 
     type: String, 
     required: true, 
     trim: true 
    }, 
    host_name: { 
     type: String, 
     required: true 
    }, 
    hosted: { 
     type: Number, 
     required: true 
    } 
}); 

// make this available to our users in our Node applications 
var Site = mongoose.model('Site', sitesEntrySchema); 
module.exports = Site; 

そして、私の角度httpリクエスト:ここでは、間違って起こっているかを示す具体的な出力なし

var express = require('express'); 
var router = express.Router(); 
var Site = require('../models/site'); 

//Return From Mongo 
router.get('/api/mongo', function(req, res) { 
    Site.find({}, function(err, sites) { 
    if (err) 
     res.send(err) 
    res.send(sites); 
    }); 
    //res.json({"yo": "yo this shit works"}); 
}); 

//Add A Site 
router.post('/api/create', function(req, res, next) { 
    //create object with form input 
    var siteData = { 
     ip: req.body.ip, 
     domain: req.body.domain, 
     wp: req.body.wp, 
     host_name: req.body.host_name, 
     hosted: req.body.hosted 
    }; 

    // use schema's 'create' method to insert doc into mongo 
    Site.create(siteData, function(error) { 
     if (error) { 
     //return next(error); 
     res.send(error); 
     } else { 
     return res.json({ message: 'Site added!' }); 
     } 
    }); 
}); 

答えて

0

は、ここに私のマングーススキーマです私にはいくつかのことがあります。 1つ目は常にjsonに応答するとは限りません。また、Expressは正しいエラー応答を送り返すため、next()を使用してエラーを処理してください。これらの変更により、あなたのGETルートは次のようになります。あなたの作成した経路は、あなたに応じて、さらに

//Add A Site 
router.post('/api/create', function(req, res, next) { 
    //create object with form input 
    var siteData = { 
     ip: req.body.ip, 
     domain: req.body.domain, 
     wp: req.body.wp, 
     host_name: req.body.host_name, 
     hosted: req.body.hosted 
    }; 

    // use schema's 'create' method to insert doc into mongo 
    Site.create(siteData, function(error, site) { 
     if (error) { 
     next(error); 
     } else { 
     return res.json(site); 
     } 
    }); 
}); 

ようになっているはずですので、

//Return From Mongo 
router.get('/api/mongo', function(req, res, next) { 
    Site.find({}, function(err, sites) { 
    if (err) { 
     next(err) 
    } else { 
     return res.json(sites); 
    } 
    }); 

}); 

は第二に、新しく作成されたリソースを返すためのベストプラクティスですAngularのバージョンでは、ポストリクエストに対して廃止予定の約定構文を使用している可能性があります。 .success()および.error()ではなく、.then()を使用している必要があります。これはまた、問題を引き起こしている可能性があります。

最後に、ルートとレスポンスのRESTガイドラインに従うことをおすすめします。それははるかに簡単にあなたのWebアプリケーションを拡張するようになり、より組織化され続けるでしょう。ここには、https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4のための良いExpress/Nodeリソースがあります。

を追加しました:ここでは、生産/開発環境に

// development error handler 
// will print stacktrace 
if (app.get('env') === 'development') { 
    app.use(function(err, req, res, next) { 
    console.log('err:', err.status, err.message); 
    res.status(err.status || 500); 
    res.json({message: err.messages}); 
    }); 
} 

// production error handler 
// no stacktraces leaked to user 
app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
    message: err.message, 
    error: {} 
    }); 
}); 
+0

ありがとう@peachesを依存するあなたのエラーをログに記録する方法の例です!私はあなたの提案に一致するように私のコードを整理し、私は今すぐドキュメントを作成することができますが、まだDBからGETできません。エラーオブジェクトを次の(エラー)時に表示するにはどうすればよいですか? – Calrocks

+0

@Calrocksおそらくlocalhost上であなたのサーバーが開発中であると仮定していますか?エラーをうまく記録する方法については、私の答えの追加を見てください。ローカルで実行している場合は、サーバーが実行されている場所であればどこにでも印刷する必要があります。 – peaches

関連する問題