Coder Social home page Coder Social logo

gotyaa's People

Contributors

flyptkarsh avatar

Watchers

 avatar  avatar

gotyaa's Issues

I need to turn a $promise object into a JSON object.

I am using $resources to make calls to my rails database. The calls work, but rather than returning a JSON object I can use they return a $promise, with all the data I need, but that I cannot iterate through.

I have been looking at various tutorials on working with promises
http://www.html5rocks.com/en/tutorials/es6/promises/
https://docs.angularjs.org/api/ng/service/$q
http://andyshora.com/promises-angularjs-explained-as-cartoon.html

What I get in Chrome

$promise: Object, $resolved: false]0: Resourcecontent: "My pants are on fire"created_at: "2014-09-01T18:00:23.276Z"id: 1updated_at: "2014-09-01T18:00:23.276Z"user_id: 1__proto__: Resource1: Resource2: Resource3: Resource4: Resource5: Resource6: Resource7: Resource8: Resource9: Resource10: Resource11: Resource12: Resource13: Resource14: Resource15: Resource16: Resource17: Resource18: Resource19: Resource$promise: Object$resolved: truelength: 20__proto__: Array[0]

My application.js

// creates the entire angular application 
var app = angular.module('GotyaaApp', ['ngResource', 'templates'])
  // configures $resource for the factories 
  .config(['$resourceProvider', function ($resourceProvider) {}])
  // factory for making ajaxy angular calls to the message database
  .factory('GotYaas', ['$resource', function($resource) {
    return $resource('http://localhost:3000/got_yaas', null,
       {});
  }])
  // controls adding the message and creating a new gotyaa 
  .controller('GotyaaController', ['$scope', 'GotYaas', function($scope, GotYaas) {
    var gotYaasPromise = GotYaas.query();
    console.log(gotYaasPromise); 
    $scope.messages = [];

    // adds the content to the message 
    $scope.submit = function() {
      if ($scope.smsContent) {
        $scope.messages.push(this.smsContent);
        $scope.smsContent = '';
      }
    };

422 Error from the server

I am getting a 422 error when I try to submit my post request.
I think it is because my post request is in HTML and not JSON but I do not know how to change it.
I have been looking at the Angular documentation for $resources.
https://docs.angularjs.org/api/ngResource/service/$resource2
Which says a post request should look like this...

     // we can create an instance as well
     var newCard = new CreditCard({number:'0123'});
     newCard.name = "Mike Smith";
     newCard.$save();
     // POST: /user/123/card {number:'0123', name:'Mike Smith'}
     // server returns: {id:789, number:'0123', name: 'Mike Smith'};
     expect(newCard.id).toEqual(789);

From my app.js

    $scope.saveMessage = function(message, current_user_id ){

       var newGotyaa = new GotYaas(); 

       newGotyaa.user_id = current_user_id; 
       newGotyaa.content = message; 
       newGotyaa.$save();
    }; 
POST http://localhost:3000/got_yaas 422 (Unprocessable Entity) angular.js?body=1:8540(anonymous function)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.