使用sails js删除记录时出现Mongo错误

时间:2015-08-13 11:05:43

标签: javascript node.js mongodb sails.js sails-mongo

我是sails.js中的新手,尝试使用mongodb和sails.js创建crud,从记录列表中删除记录时遇到问题。在这里,我附上我的所有工作文件,请指导我

// 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');

    }

这是我的ejs文件

 <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>

这是我的路线

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

点击删除链接时,它调用控制器函数removePost()

终端错误显示:

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']

任何帮助都将被评估 提前致谢

1 个答案:

答案 0 :(得分:1)

在控制器中,您使用的是Dashboard.remove(),但此方法不存在。 Use Dashboard.destroy().exec() instead

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