Reputation: 1160
factory:
angular.module('clientApp').factory('Di', function($http) {
return {
create: function(dis){
return $http.post('/dis', dis);
}
});
Controller:
'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= {};
});
};
});
When I console.log() the response, the only thing I see in response.data is the hashKey. I do see the object in response.config.data, but from what I've seen online, this is just the original object i'm sending to the database, not the returned promise, right?
The data is saving to the database.
What am I doing wrong? I've done a similar configuration successfully with other promises, but the response is not what I'm expecting here.
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);
controller:
'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});
});
};
Routes:
var dis = require('../contollers/dis');
app.post('/dis', dis.create);
Upvotes: 0
Views: 701
Reputation: 353
You had a typo with an extra parameter within your create function.
exports.create = function(req, res){
Di.create(req.body , function(err, di){
console.log('req.body.di', req.body);
res.send({di:di});
});
};
Upvotes: 2
Reputation: 1849
I think you should bind your promise to the scope. Would that fix the problem? Can you try?
$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));
};
Upvotes: 0