mas cm
mas cm

Reputation: 131

Logic error when I try to create a new record in sails.js

When I want to create a new record in sails.js, I receive a logical error. When I delete the following lines in my Controller, it's working:

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,  

My route is:

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

My model is:

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

My controller is:

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});
    });
   },  

I use postman to send the request as follows:

Screenshot of postman

But I receive the JSON {"status": false} and no record is created in the database. How can I fix my problem?

Upvotes: 0

Views: 46

Answers (1)

Lucas Penén
Lucas Penén

Reputation: 51

Some things to clarify:

Can you show the error? You can do that adding it to the json response.

If you put a defaultsTo property in the model, you shouldnt put it in the controller.

You have a lot of associations, so you can't send 0 as value of that associations because 0 is not a valid identifier. You should put a valid id or undefined.

Upvotes: 1

Related Questions