Loopback: How to control document do not get or add additional property in loopbak?

Created on 8 Oct 2017  路  4Comments  路  Source: strongloop/loopback

I have a registration model in loopback over mongodb with fallowing properties:

    "properties": {
        "Fname": {
          "type": "string",
          "required": true
        },
        "Lname": {
          "type": "string",
          "required": true
        },
        "phone": {
          "type": "string",
          "required": true
        },
        "date": {
          "type": "string",
          "required": true
        },
        "time": {
          "type": "string",
          "required": true
        }
      }

in application I post some additional data with model required data, for controlling and processing in server side:

```
submitForm() {

    let headers = new Headers(
      {
        'Content-Type': 'application/json'
      });
    let options = new RequestOptions({ headers: headers });

    let data = JSON.stringify({
      Fname: this.form.Fname,
      Lname: this.form.Lname,
      phone: this.form.phone,
      time: this.form.time,
      date: this.form.date,
      uid: this.form.uid
    });
    //console.log(data);

    let url = 'http://localhost:3000/api/registrations';

    return new Promise((resolve, reject) => {
      this.http.post(url, data, options)
        .toPromise()
        .then((response) => {
          console.log('API Response : ', response.status);
          resolve(response.json());
        })
        .catch((error) => {
          console.error('API Error : ', error.status);
          console.error('API Error : ', JSON.stringify(error));
          reject(error.json());
        });
    });
  }
In server side I have this code:

     Registration.observe('before save', function (ctx, next) {
        ```
    if (ctx.instance) {
                // When Create (POST)
                // ctx.instance have the json properties
                console.log("Triggers when create");
                if (checkUID(ctx.instance) ==200 ){
                    console.log('ok');
                }
            } else {
                // When Update (UPDATE)
                // ctx.data have the json properties
                console.log("Triggers when update");
            }
            next();
        });

but after successfully registration I saw that uid that was added into document and regardless to model's properties, document contains addition properties.

    {
        "Fname": "Eram",
        "Lname": "SA",
        "phone": "1234567890",
        "date": "2017/10/06",
        "time": "17:37:46",
        "id": "59d78e3f5e5e6704205038aa",
        "uid": "38bc3241a43073a7b40d186f24923cc5"
      },

Most helpful comment

use strict:true in your model properties

{
  "name": "Customer",  // See Top-level properties below

  "description": "A Customer model representing our customers.",
  "base": "User",
  "idInjection": false,
  "strict": true,
. . . 
}

All 4 comments

use strict:true in your model properties

{
  "name": "Customer",  // See Top-level properties below

  "description": "A Customer model representing our customers.",
  "base": "User",
  "idInjection": false,
  "strict": true,
. . . 
}

Dear @osamasaeed by using strict:true there is problem happening that says :`uid is not defined in the modelhoweveruid` is not defind in registration model but we need this uid on server side to verify the new registration.

If you just want to use 'uid' attribute then you can remove it after verify in the beforeRemote hook e.g
in myModel.js

. . . 
Mymodel.beforeRemote('create',function(context, data, next){
// verifying 
...
// after verify
  delete data.uid
  return next();
})
. . .

There are many remote hooks you can find on loopback documentation
https://loopback.io/doc/en/lb3/Remote-hooks.html
more detail about accessing relational model Remotehooks
https://loopback.io/doc/en/lb2/Accessing-related-models.html
to use these relational model remote hooks use prefix "prototype." for example

Mymodel.beforeMethod("prototype.__get__relatedModelName",function(ctx,data,next){
next()
})

I got solution from your guiding,
I've add a variable for UID after storing it. removed attribute by

UID = ctx.instance.uid;
ctx.instance.unsetAttribute('uid');

Was this page helpful?
0 / 5 - 0 ratings