2017-01-30 58 views
-1

我試圖在API接口上調用多個$http來更新我的模型。
讓我解釋一下。我想將一個新對象保存到我的數據庫中,並且一旦保存了該對象,我希望返回該對象並執行另一個更新另一個對象的調用$http。這裏是我的代碼:

型號:

var departmentSchema = mongoose.Schema({ 
    username  : { type:String,unique:true,required:true}, 
    email  : { type:String,unique:true,required:true}, 
    name   : { type:String, default:"", unique:true,required:true}, 
    stations  : { 
     type: [mongoose.Schema.Types.ObjectId], 
     ref: 'Station' 
    } 
}) 

// Method that Pushes a Station to the department 
departmentSchema.methods.pushStation = function(station) { 
    var department = this; 
    department.stations.push(station) 
}; 

API路線

router.put('/departments/:dep_id/stations/:stat_id', function(req, res, next) { 
    Station.findOne({_id: req.params.stat_id}, function(err, data1){ 
     if (err) { return next(err); } 

     var station = data1; 
     Department.findOne({_id: req.params.dep_id}, function(err, data2){ 
      if (err) { return next(err); } 

      var department = data2; 
      department.pushStation(station) 
      res.json({success:true, department:department}); 
     }); 
    }); 
}); 

Angularjs $ HTTP縮進呼叫

$scope.addNewStation = function(station){ 
    $http.post('/api/stations/', station) 
    .then(function (data) { 
     console.log(data) 
     $scope.station = data.station; 
     $http.put('/api/departments/' + $scope.department._id + '/stations/' + $scope.station._id, $scope.station) 
     .then(function (data) { 
      console.log(data); 
     }) 
     bootbox.alert("Station Created Successfully"); 

    },function (err) { 
     console.log(err) 
    }) 
} 

我應該指出,我的網址中有$scope.department,這是因爲我從以前的調用中獲取了這些數據,並且我不想用不必要的代碼來擠佔本節。
所以問題是,當我執行$scope.addNewStation(...),我能夠成功地添加新的站,出現bootbox警報,顯示第一個console.log(data),但然後我在控制檯上得到一個錯誤:TypeError: Cannot read property '_id' of undefined和第二個console.log(data) doesn沒有出現。
請告訴我我在這做錯了什麼。我真的需要幫助。謝謝。

+0

對不起但是。無法看到設置的scope.department._id –

+0

而不是嵌套的調用,爲什麼不使用「瀑布」? [https://www.npmjs.com/package/async-waterfall] –

+0

嗨@federicoscamuzzi,就像我上面解釋過的那樣,$ scope.department是一個從前一次調用中返回的對象。該對象包含$ scope.department._id。所以只是假設它在那裏。 – AllJs

回答

1

.then()回調中的對象是一個響應對象,它包含狀態,數據和其他屬性。你應該沿着以下幾點做點什麼:

$http.post('/api/stations/', station) 
.then(function(response) { 
    $scope.station = response.data.station; 
}) 
+0

是Fissio。你的建議回答了我的問題。謝謝你的幫助。 – AllJs

0

嘗試使用角度承諾,同時檢查範圍變量工作站和部門是否正確初始化。

var promise = $http.post('/api/stations/', station); 

promise.then(
    function(data) { 
    $scope.station = data.station; 
     console.log($scope.station, $scope.department); 
     $http.put('/api/departments/' + $scope.department._id + '/stations/' + $scope.station._id, $scope.station) 
     .then(function (data) { 
      console.log(data); 
     }); 
}); 
0

我會建議使用async waterfall.

下面是一個例子(後端):

var waterfall = require('async-waterfall'); 
waterfall([ 
    function(callback){ 
     callback(null, 'param1');//the first parameter is the error - if there is one 
    }, 
    function(param2, callback){ 
     callback(null, 'param2'); 
    } 
], function(err, result){ 
    // if no errors send response back to angular 
}) 

這樣你就可以不嵌套調用做。有關更好的示例,請參閱我提供的鏈接。

相關問題