2
votes

I am new to MEAN applications.Here I have a REST based sample application using node-restful library in which I can perform operations(get,save,delete) except 'put'. However 'put' operation works well on rest clients (advanced REST, postman) but not on angular client.

mongoose Model



        var restful = require('node-restful');
        var mongoose = restful.mongoose;

    // Schema
        var productSchema = new mongoose.Schema({
          name: String,
          college: String,
          age: Number
        });

    // Return model
    module.exports = restful.model('Products', productSchema);

Node-express code



     var express = require('express');
        var methodOverride = require('method-override');
        var mongoose = require('mongoose');
        var bodyParser = require('body-parser');
        var cors =require('cors');


    // MongoDB

        mongoose.connect('mongodb://localhost/rest_test');
        var autoIncrement = require('mongoose-auto-increment');

        // Express
        var app = express();
        app.use(methodOverride('_method'));
        app.use(cors());
        app.use(bodyParser.urlencoded({ extended: true }));
        app.use(bodyParser.json());

        // Routes
        app.use('/api', require('./routes/api'));

        // Start server
        app.listen(4000);
        console.log('API is running on port 4000');
    

angular function to update the data



     $scope.updateData = function (userID) {

                $scope.actionData = {
                    "name": $scope.name,
                    "college": $scope.college,
                    "age": $scope.age
                }


                RoleEditF.updateUserData({
                    userId: userID
                }, $scope.actionData).then(function (response) {
                    $scope.userData = response;
                    console.log($scope.userData)
                    $scope.getData();
                }).catch(function (response) {
                     $scope.error = "Unable to get Files (code: " + response.status + "). Please try later.";
                });
            }



    angular.module('myapp')
            .factory('RoleEditF', function (updateS) {
                return {

                    updateUserData: function (parm, data, callback) {
                        var cb = callback || angular.noop;

                        return updateS.save(parm, data,
                            function (res) {
                                return cb(res);
                            },
                            function (err) {
                                return cb(err);
                            }.bind(this)).$promise;
                    } 
                }
            })


 

Factory to call API



     angular.module('myapp')
        .factory('updateS',function($resource) {
          return $resource('http://localhost:4000/api/products/:userId', { userId: '@userId' }, {
            update: {
              method: 'PUT' 
            }
          }, {
            stripTrailingSlashes: false
          });
        });

I'm getting following error on browser

"NetworkError: 404 Not Found - http://localhost:4000/api/products/57161e0fe4fbae354407baa3"

1
yes,It works on postmanNagendra Shanbhag

1 Answers

2
votes

it has to be 'update' in

        'update': {
          method: 'PUT' 
        }

inside your $resource() factory

documentation here https://docs.angularjs.org/api/ngResource/service/$resource

under Creating a custom 'PUT' request