尝试在sails.js中创建新记录时出现逻辑错误

时间:2018-08-02 07:47:25

标签: node.js sails.js

当我想在sails.js中创建新记录时,收到一个逻辑错误。 当我删除Controller中的以下行时,它正在工作:

amaliat_owner: req.param('amaliat_id') || 0,
place_die_owner: req.param('place_die_id') || 0,
media_place_die_owner: req.param('media_place_die_id') || 0,
media_owner: req.param('media_id') || null,
media_amaliat_owner: req.param('media_amaliat_id') || 0,  

我的路线是:

'POST /delneveshte/create': 'DelneveshteController.create'

我的模型是:

module.exports = {

attributes: {

body: {
  type: 'string',
  columnType: 'text'
},

date_time: {
  type: 'string',
},

is_confirmed: {
  type: 'boolean',
  defaultsTo: false
},

likee: {
  type: 'number',
  columnType: 'integer',
  defaultsTo: 0
},

favorite: {
  type: 'number',
  columnType: 'integer',
  defaultsTo: 0
},

user_owner: {
  model: 'user',
},

shahid_owner: {
  model: 'shahid',
},

amaliat_owner: {
  model: 'amaliat',
},

media_place_die_owner: {
  model: 'mediaplacedie',
},

place_die_owner: {
  model: 'placedie',
},

media_owner: {
  model: 'media',
},

media_amaliat_owner: {
  model: 'mediaamaliat',
},

user_delneveshte_favorites: {
collection: 'userdelneveshtefavorite',
via: 'delneveshte_owner'
},

user_delneveshte_likes: {
collection: 'userdelneveshtelike',
via: 'delneveshte_owner'
},  
},  
};

我的控制器是:

create: async function (req, res, next) {
    console.log(req.param('place_die_id'));
    Delneveshte.create({
      body: req.param('body'),
      date_time: req.param('date_time'),
      likee: req.param('like') || 0,
      favorite: req.param('favorite') || 0,
      is_confirmed: req.param('is_confirmed') || 0,
      user_owner: req.param('user_id'),
      shahid_owner: req.param('shahid_id') || 0,
      amaliat_owner: req.param('amaliat_id') || 0,
      place_die_owner: req.param('place_die_id') || 0,
      media_place_die_owner: req.param('media_place_die_id') || 0,
      media_owner: req.param('media_id') || null,
      media_amaliat_owner: req.param('media_amaliat_id') || 0,
    }, function (err, new_del) {
      console.log("1");
      if(err){
        console.log("2");
        return res.json({'status':false});
      }
      console.log("2");
      return res.json({'status':true, 'result':new_del});
    });
   },  

我使用邮递员发送请求,如下所示:

Screenshot of postman

但是我收到JSON {"status": false},并且在数据库中未创建任何记录。我该如何解决我的问题?

1 个答案:

答案 0 :(得分:1)

需要澄清的一些事情

您可以显示错误吗?您可以将其添加到json响应中。

如果在模型中放置defaultsTo属性,则不应将其放置在控制器中。

您有很多关联,因此您不能发送0作为该关联的值,因为0不是有效的标识符。您应该输入有效的ID或未定义的