dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 379 forks source link

.save method is not found in many to many relationship #729

Open noopuraspire opened 8 years ago

noopuraspire commented 8 years ago

I've established many to many relationship between them.

User Model:

User.hasMany(
    'SERVICE',
    db.models.SERVICE,
    {
      price            : { type: 'number' },
      type_id          : { type: 'number' },
      isRemoved        : { type: 'boolean' },
      serviceCreatedAt : { type: 'date', time: true },
      serviceUpdatedAt : { type: 'date', time: true }
    },
    {
      autoFetch : true,
      reverse   : 'user'
    });

Based on documents, I should be able to use,

user.getSERVICE();
user.addSERVICE();
user.setSERVICE();
user.hasSERVICE();
user.removeSERVICE();

Everything is working perfectly, but while using method user.setSERVICE(), I'm getting error :

uncaughtException: Association.save is not a function.

Below is code in which I'm using .setSERVICE() method:

req.models.SERVICE.exists({ id: currService.serviceId }, function (err, exists) {
  if(exists) {
    // get service based on service id
    req.models.SERVICE.get(currService.serviceId, function (err, service) {
      if (err) {
        helpers.util.sendError(err, res, next);
      }
      user.setSERVICE(
        service,
        {
          type_id          : currService.typeId,
          price            : currService.price,
          isRemoved        : false,
          serviceCreatedAt : new Date(),
          serviceUpdatedAt : new Date()
        }, function(err, user) {
        if (err) {
          helpers.util.sendError(err, res, next);
        }
        console.log(user);
      });
    });
  } else {
    // service not found, return error in response
    return res.status(helpers.error.status.BadRequest).send({ error: helpers.error.message.NotFound });
  }
});

Logs :

[SQL/mysql] SELECT `id`, `service`, `isActivate`, `isDeleted`, `createdAt`, `updatedAt` FROM `SERVICE` WHERE `id` = 2 LIMIT 1
[SQL/mysql] SELECT `id`, `service`, `isActivate`, `isDeleted`, `createdAt`, `updatedAt` FROM `SERVICE` WHERE `id` = 2 LIMIT 1
[SQL/mysql] SELECT `t1`.`id`, `t1`.`firstname`, `t1`.`lastname`, `t1`.`username`, `t1`.`password`, `t1`.`email`, `t1`.`pharmacyNo`, `t1`.`isActivate`, `t1`.`isDeleted`, `t1`.`isEulaAccepted`, `t1`.`createdAt`, `t1`.`updatedAt`, `t1`.`role_id`, `t2`.`price`, `t2`.`type_id`, `t2`.`isRemoved`, `t2`.`serviceCreatedAt`, `t2`.`serviceUpdatedAt` FROM `USER` `t1` JOIN `USER_SERVICE` `t2` ON `t2`.`user_id` = `t1`.`id` WHERE `t2`.`service_id` = 2
[SQL/mysql] SELECT `t1`.`id`, `t1`.`firstname`, `t1`.`lastname`, `t1`.`username`, `t1`.`password`, `t1`.`email`, `t1`.`pharmacyNo`, `t1`.`isActivate`, `t1`.`isDeleted`, `t1`.`isEulaAccepted`, `t1`.`createdAt`, `t1`.`updatedAt`, `t1`.`role_id`, `t2`.`price`, `t2`.`type_id`, `t2`.`isRemoved`, `t2`.`serviceCreatedAt`, `t2`.`serviceUpdatedAt` FROM `USER` `t1` JOIN `USER_SERVICE` `t2` ON `t2`.`user_id` = `t1`.`id` WHERE `t2`.`service_id` = 2
[SQL/mysql] DELETE FROM `USER_SERVICE` WHERE `user_id` = 5

After removing entries from DB, it is not adding entries, and giving error : uncaughtException: Association.save is not a function.

Please, tell me where I'm going wrong.