2016-05-05 8 views
0

工場:角度約束response.dataは空です

angular.module('clientApp').factory('Di', function($http) { 
    return { 
    create: function(dis){ 
     return $http.post('/dis', dis); 
    } 
}); 

コントローラ:私は()の応答をCONSOLE.LOG

'use strict'; 

angular.module('clientApp').controller('AdminCtrl', function($scope, toastr, Di) { 
    $scope.di = {}; 
    $scope.dis = []; 

    $scope.add = function(){ 
     Di.create($scope.di).then(function(response){ 
     console.log(response, 'front data post') 
     $scope.dis.push(response.data); 
     $scope.di= {}; 
     }); 
    }; 
    }); 

が、私はresponse.dataで見るだけの事はハッシュキーであります。私はresponse.config.dataのオブジェクトを見ていますが、私がオンラインで見たものから、これは返された約束ではなく、データベースに送信している元のオブジェクトです。

データはデータベースに保存されています。

私は間違っていますか?私は他の約束と同様の設定を成功させましたが、ここで期待しているのはレスポンスではありません。

API

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var DiSchema = new mongoose.Schema({ 
    name: { type: String, lowercase: true , required: true }, 
    photo: { type: String }, 
    email: { type: String, unique: true, lowercase: true }, 
    year: { type: Number}, 
    timestamp: { type : Date, default: Date.now }, 
    description: { type: String}, 
    location: { type: Array }, 
    social: { 
    website: {type: String}, 
    facebook: {type: String }, 
    twitter: {type: String }, 
    instagram: {type: String } 
    } 
}); 

DiSchema.methods.create = function(o, cb){ 
    this.model.save(o, cb); 
}; 

module.exports = mongoose.model('Di', DiSchema); 

コントローラ:

'use strict'; 

var Di = require('../models/di'); 

exports.create = function(req, res){ 
    Di.create(req.body , user, function(err, di){ 
    console.log('req.body.di', req.body); 
    res.send({di:di}); 
    }); 
}; 

ルート:

var dis = require('../contollers/dis'); 
app.post('/dis', dis.create); 
+2

これは問題の可能性があります。サービス上の 'create'メソッドは、' di'ではなく 'dis'という名前のパラメータを持つべきです。あなたは 'dis'を投稿に渡しています。これは未定義です。 – DerekMT12

+0

これは 'post'か' get'ですか? – ste2425

+1

それは、 'function(di){}'を作成しています – Dan

答えて

2

あなたは、作成機能内に余分なパラメータを入力していました。

exports.create = function(req, res){ 
    Di.create(req.body , function(err, di){ 
    console.log('req.body.di', req.body); 
    res.send({di:di}); 
    }); 
}; 
0

私はあなたがスコープにあなたの約束をバインドするべきだと思います。 問題を修正できますか?あなたは試みることができますか?

$scope.add = function(){ 
    Di.create($scope.di).then(function(response){ 
    console.log(response, 'front data post') 
    $scope.dis.push(response.data); 
    $scope.di= {}; 
    }.bind($scope)); 
}; 
関連する問題