删除任何记录时出现Sails.js错误

时间:2021-05-03 15:39:46

I am new in sails js , Here i am follow one tutorial to create crud using sails js and mongodb.

我是sails js的新手,在这里,我遵循一个教程,使用sails js和mongodb创建crud。

But while deleting any record getting 500 internal server error console error

但删除任何记录时获得500内部服务器错误控制台错误

"NetworkError: 500 Internal Server Error - http://localhost:1337/user/delete/55bf315ee3437a512628916b"

Here is my controller file delete function:

这是我的控制器文件删除功能:

 delete: function(req, res) {  

    var id=req.param("id",null);

      User.findOne(id).done(function(err, user) {

    // we now have a model with instance methods attached

    // destroy the record
    user.destroy(function(err) {

      res.redirect( 'user/index/');

      // record has been removed
    });

  });
  }

Here is my view

这是我的看法

<a href="/user/create">+Create</a>
<ol>
<% users.forEach( function( model ){ %>
  <li><%= model.name %>(<a href="/user/delete/<%= model.id %>">delete</a>||<a href="/user/update/<%= model.id %>">Update</a>||<a href="/user/view/<%=model.id %>">view</a>)</li>
<% }); %>
</ol>

// Here is my route file

//这是我的路线档案

module.exports.routes = {

  '/': {
    view: 'homepage'
  },
  'post/User':{
    view: 'user/create'
  }


};

Please guide me i dont't know where i am doing mistake.

请指导我,我不知道我在哪里做错了。

Thanks in advance

提前致谢

2 个解决方案

#1


1  

I think the mistake could be in the res.redirect, you should place it outside destroy() function.

我认为错误可能在res.redirect中,你应该把它放在destroy()函数之外。

delete: function(req, res) {

delete:function(req,res){

var id=req.param("id",null);

  User.findOne(id).done(function(err, user) {

// we now have a model with instance methods attached

// destroy the record
user.destroy(function(err) {

  if (err) return

});
res.redirect( 'user/index/');

}); }

#2


1  

I was fixed the error , its come since i am using

我修正了错误,因为我正在使用它

    User.findOne(id).done(function(err, user) {
      ..
    }

Insted of this :

绝对是这样的:

 User.findOne(id).exec(function(err, user) {
  ..             ^^^^^^
 }

#1


1  

I think the mistake could be in the res.redirect, you should place it outside destroy() function.

我认为错误可能在res.redirect中,你应该把它放在destroy()函数之外。

delete: function(req, res) {

delete:function(req,res){

var id=req.param("id",null);

  User.findOne(id).done(function(err, user) {

// we now have a model with instance methods attached

// destroy the record
user.destroy(function(err) {

  if (err) return

});
res.redirect( 'user/index/');

}); }

#2


1  

I was fixed the error , its come since i am using

我修正了错误,因为我正在使用它

    User.findOne(id).done(function(err, user) {
      ..
    }

Insted of this :

绝对是这样的:

 User.findOne(id).exec(function(err, user) {
  ..             ^^^^^^
 }