Search code examples
javascriptnode.jsmongodbsails.jssails-mongo

Mongo Error while removing record using sails js


I am new in sails.js , trying to create crud using mongodb and sails.js , Getting some issue while delete a record from the list of record. Here i attach my all working file , Please guide me

// controller file function 
removePost: function(req, res)
    {

        var id = req.query.id;
        //var id = req.param("id", null);
        console.log(id);
        sails.log(id);
        Dashboard.remove({'_id' : id } , function(error){
            console.log("Come in dataase" + error);
        })
        res.redirect('dashboard/index');

    }

Here is my ejs file

 <div class="col-sm-9">
            <h3>Latest Post</h3>
            <% result.forEach( function( model){ %>
            <br>
            <div class="media">
               <i class="fa fa-check-square-o pull-left fa-2x"></i>
               <a href="/dashboard/removePost?id=<%=model.id%>"><i class="fa fa-trash-o pull-left fa-2x"></i></a>
               <div class="media-body">
                  <h3 class="media-heading"> <%=model.title %> </h3>
                  <%=model.description %>
               </div>
            </div>
            <% }); %>

         </div>

Here is my route

module.exports.routes = {

  '/': {
    view: 'homepage'
  },
  '/about':{
    view: 'about/index'
  },
  'get /register': 'RegisterController.index',
  'post /register/create' : 'RegisterController.create',
  'get /dashboard/create' : 'DashboardController.create',
  'get /Dashboard': 'DashboardController.index',
  'get /dashboard/removePost:id': 'DashboardController.removePost'
};

while click on delete link it call above controller function removePost()

And the terminal error shows:

error: Sending 500 ("Server Error") response: 
 TypeError: Object #<bound> has no method 'remove'
    at Object.module.exports.removePost (/var/www/html/sailsTemplate/api/controllers/DashboardController.js:49:13)
    at bound (/usr/lib/node_modules/sails/node_modules/lodash/dist/lodash.js:729:21)
    at routeTargetFnWrapper (/usr/lib/node_modules/sails/lib/router/bind.js:179:5)
    at callbacks (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at param (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:135:11)
    at pass (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at /usr/lib/node_modules/sails/lib/router/bind.js:187:7
    at alwaysAllow (/usr/lib/node_modules/sails/lib/hooks/policies/index.js:207:11)
    at routeTargetFnWrapper (/usr/lib/node_modules/sails/lib/router/bind.js:179:5)
    at callbacks (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at param (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:135:11)
    at pass (/usr/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5) [TypeError: Object #<bound> has no method 'remove']

Anyone help will be apprciated Thanks in advance


Solution

  • In the controller, you are using Dashboard.remove() but this method does not exists. Use Dashboard.destroy().exec() instead.

    Dashboard.destroy({'_id' : id }).exec(function(err){
        console.log('The record has been deleted');
    })