loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Report about Hook event is not available

Open bluestaralone opened this issue 12 years ago • 10 comments

Hi,

I have a problem when making request from client (PUT method). I can't hook event beforeSave of Model in this case. Please tell me know how i can do it ?

bluestaralone avatar Sep 18 '14 02:09 bluestaralone

I have tested everything work find on PUT. (loopback v2.9.1)

module.exports = function(Blog) {
    Blog.beforeSave = function(next,data)
        console.log('hello');
        next()
    }
}

veekungx avatar Sep 18 '14 03:09 veekungx

Where can i get loopback v2.9.1 ?, in my app it's only 2.2.0

bluestaralone avatar Sep 18 '14 04:09 bluestaralone

Sorry for mistake. you can check with

slc version 

loopback 2.2.0 is correct.

slc update

veekungx avatar Sep 18 '14 05:09 veekungx

Here is my result with slc version: strongloop v2.9.1 (node v0.10.32) ├── [email protected] ├─┬ [email protected] │ └── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] └── [email protected]

Can you give me example about using beforeSave on PUT method ?

bluestaralone avatar Sep 18 '14 05:09 bluestaralone

"blog.json"

{
  "name": "Blog",
  "plural": "Blogs",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "properties": {},
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

"blog.js"

module.exports = function(Blog) {


    Blog.beforeSave = function(next,data){

        console.log('hello');
        next()
    }

};

I got console.log print out when call PUT /Blogs/{pk} PUT /Blogs

veekungx avatar Sep 18 '14 05:09 veekungx

i put a console.log('aa') on function beforeSave() but don't get any text show on my console when i request PUT method to server. Please give me a complete example that implement this case. Thanks a lot.

bluestaralone avatar Sep 18 '14 05:09 bluestaralone

slc loopback:model

[?] Enter the model name: Joker
[?] Select the data-source to attach Joker to: db (memory)     
[?] Expose Joker via the REST API? Yes  
[?] Custom plural form (used to build REST URL):
Let's add some Joker properties now.

Enter an empty property name when done.
[?] Property name:

then you got "joker.json" , "joker.js" edit "joker.js"

module.exports = function(Joker) {
    Joker.beforeSave = function(next,data){
        console.log(data)
        console.log('Hello Joker');
        next()
    }
};

Call (I use loopback explorer) PUT /Jokers

{
    "name" : "joker I"
}

It works fine.

Here my ss http://postimg.org/image/q63pr8l9b/

veekungx avatar Sep 18 '14 05:09 veekungx

I recreate new Model as you comment it work fine but it doesn't work when you create Model with property name is "location".

{ "name": "Store", "plural": "Stores", "base": "PersistedModel", "properties": { "name": { "type": "string", "length": 50, "required": true }, "location": { "type": "geopoint", "required": true }, "picture": { "type": "string" } }, "validations": [], "relations": {}, "acls": [], "methods": [] }

You can try to create a new model with upper structure and you can see this bug.

bluestaralone avatar Sep 18 '14 08:09 bluestaralone

I have no problem. Here ss http://postimg.org/image/dl719rlu7/

veekungx avatar Sep 18 '14 09:09 veekungx

the problem doesn't happen anymore after I restarted my computer - Ubuntu 12.04.5 LTS O.o

thanks for ur support :">

bluestaralone avatar Sep 18 '14 10:09 bluestaralone