0

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);
9
  • 2
    You have a typo, which may be the issue. Your create method on the service should have the parameter named dis instead of di. You're passing dis to the post, which is undefined. Commented May 5, 2016 at 14:56
  • Sorry is this the post or get? Commented May 5, 2016 at 14:57
  • 1
    It's the create function(di){} Commented May 5, 2016 at 14:59
  • yeah, it was confusing how I had it. I've corrected it. It was the post. Thank you for pointing out the typo. I do have 'dis' in my actual code. It still results in only the hashkey within response.data. Commented May 5, 2016 at 15:07
  • what are you sending back from the api? It might help for us to see the code for that too. Commented May 5, 2016 at 15:09

2 Answers 2

2
+50

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});
  });
};
Sign up to request clarification or add additional context in comments.

Comments

0

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));
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.