私のルートに問題があり、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!' });
}
});
});
ありがとう@peachesを依存するあなたのエラーをログに記録する方法の例です!私はあなたの提案に一致するように私のコードを整理し、私は今すぐドキュメントを作成することができますが、まだDBからGETできません。エラーオブジェクトを次の(エラー)時に表示するにはどうすればよいですか? – Calrocks
@Calrocksおそらくlocalhost上であなたのサーバーが開発中であると仮定していますか?エラーをうまく記録する方法については、私の答えの追加を見てください。ローカルで実行している場合は、サーバーが実行されている場所であればどこにでも印刷する必要があります。 – peaches