1

I have a list of categories and a list of places, each place has an categorieId in foreign key.

categorie.json: http://pastebin.com/ttumKPf9

place.json: http://pastebin.com/J4bdEiUx

I try to verify if the category id exists. For this, i redefine a create method in place class. In place class I search if the category id is present in category. If i find it, i call upsert method for insert the data in place. But this upsert method recall create method... If i don't call upsert method, the data isn't insert. How can I verify and insert the data in strongloop?

script.js:

module.exports = function(app){

 var Place = app.models.place;
 var Categorie = app.models.categorie;

 Place.create = function(data,callback){
  console.log("avant find");

    console.log(data.id);

    Categorie.findById(data.categorieId, function(err, instance){
      console.log("find")

      if(err){
        throw err;
      }
      if(instance == null){
        new err;
        throw err;
      }

      console.log("Upsert");
      Place.upsert(data, callback);
    });  
 };
};

1 Answers1

1

You should redefine remote method, not the ORM method.

Another approach, if you use relational DB, create foreign key constraints.

Here is the SQL query, where place and categorie are table names: ALTER TABLE place ADD CONSTRAINT Categorie_places_ix FOREIGN KEY (categorieId) REFERENCES categorie(id) ON DELETE SET NULL;

IvanZh
  • 2,265
  • 1
  • 18
  • 26