Friday, 21 April 2017

Loopback unable to create and do operations on related models in model JS file

Unable to create relation in model.js file while through model.json file, it's working fine. I want to update booking table field on any updation in bidding table but my relation is not working in model.js file.

Bidding.belongsTo(Booking, {foreignKey: 'bookingId'}); ^ ReferenceError: Booking is not defined

{
  "error": {
    "statusCode": 500,
    "name": "TypeError",
    "message": "Bidding.booking is not a function",
    "stack": "TypeError: Bidding.booking is not a function\n.."
  }
}  
'use strict';


 //var loopback = require('loopback');
//var boot = require('loopback-boot');

//var app = module.exports = loopback();

module.exports = function(Bidding) {

          // var app = require('../../server/server'); 
    // var Booking = app.models.Booking;
    //Bidding.belongsTo(Myuser, {foreignKey: 'driver_id'});

    Bidding.belongsTo(Booking, {foreignKey: 'bookingId'});

    Bidding.observe('before save', function beforeSave(ctx, next) {
                if (ctx.instance) {

                   //on create
                   ctx.instance.created = new Date();
                   ctx.instance.modified = new Date();

                } else {
                   // on edit
                // ctx.instance.lastUpdated = new Date();
                console.log('updatesdd');
                //Bidding.Booking.upsertWithWhere({id: ctx.instance.id},{ 'username': username}, function(err, results) {});
                Bidding.booking(function(err, booking) {
                 
                         console.log(ctx.booking);      
                     });
                }

               
                next();
            });

        Bidding.observe('loaded', function beforeaccess(ctx, next) {

           console.log(ctx.data);


               
                next();
            });

};


via user2968961

No comments:

Post a Comment