problems with controller, models and sparkpost.cfc

71 views
Skip to first unread message

Ivan

unread,
Apr 1, 2020, 4:32:58 PM4/1/20
to framework-one
Hi everyone,
i am taking the first steps with fw1 and i have a problem that i can't solve.



this is (a part) of application.cfc

 diConfig = {
    loadListener
= "LoadListener",
    exclude
: [ '/com/' ]
 
},
 
 routes
= [
   
// ...
   
{ "$GET/:category/:subcategory/:slug" = "/product/show/category/:category/subcategory/:subcategory/slug/:slug" }
   
// ....
 
]




model/LoadListener.cfc

 component {
public function onLoad( beanfactory ){

beanfactory

.declare( "_" ).aliasFor( "underscoreUtil" ).done()

.declare( 'postmark' ).instanceOf( 'com.postmark.postmark' ).asSingleton().withOverrides(
{
accountToken = 'xxxx',
serverToken = 'yyyy',
}
).done()


.declare("BaseGrammar").instanceOf("qb.models.Grammars.BaseGrammar").done()
.declare("MySQLGrammar").instanceOf("qb.models.Grammars.MySQLGrammar").done()
.declare("QueryUtils").instanceOf("qb.models.Query.QueryUtils").done()
.declare("query").instanceOf("qb.models.Query.QueryBuilder")
.withOverrides({
grammar: beanfactory.getBean("MySQLGrammar"),
utils: beanfactory.getBean("QueryUtils"),
returnFormat: "query"
}).done()
.declare("SchemaBuilder").instanceOf("qb.models.Schema.SchemaBuilder")
.asTransient()
.withOverrides({
grammar: beanfactory.getBean("MySQLGrammar")
})

;

beanFactory.load();

}
}



controllers/product.cfc

component name="product" output="false" accessors="true"{


 property beanFactory
;
 property productService
;
 property categoryService
;
 property securityUtil
;
 property _
;


 
public any function init( fw ) {
   variables
.fw = arguments.fw;
   
return this;
 
}


 
public function default( struct rc ){

 
}


 
public function show( struct rc ){

   variables
.productService.sendEmailTest( to = "fr...@email.it", text = "test" );

 
}



}



models/services/product.cfc

component output="false" accessors="true" {


 property
SchemaBuilder;
 property query
;
 property securityUtil
;
 property postmark
;


 
public any function getProductBySlug(required string slug){
 
//
 
}

 
public function getProductByID(required string productId){
 
//
 
}


 
public function sendEmailTest( required string to, required string text){
  test
= variables.postmark.sendEmail(
   
From        = "m...@email.it",
   
To          = arguments.to,
    subject    
= "subjecttest",
   
TextBody    = arguments.text,
   
HtmlBody    = arguments.text
 
);
 
}


}



/com/postmark/postamark.cfc




The problem is when I call the `show()` controller, 6 emails are sent with the sparkpost service. It should send only one. Where am I doing wrong?

Matthew Clemente

unread,
Apr 1, 2020, 4:54:13 PM4/1/20
to framework-one
Is `show()` being called more than once (intentionally or not)? That is, if you `abort` within `show()`, after sending the email, is it still sent 6 times?

Ivan

unread,
Apr 1, 2020, 5:08:23 PM4/1/20
to framework-one
Hi Matthew,

  • the `show()` function of the controller is called via url, when the page is loaded (`website.com/category/subcategory/product`)
  • I tried to insert an `abort;` before closing the `show()` function and this time only one email is sent!

Mmmhh, I'm doing something wrong.


errata corrige: I wrote 'sparkpost' but I wanted to write 'postmark'.

Matthew Clemente

unread,
Apr 1, 2020, 5:12:50 PM4/1/20
to framework-one
So, your controller isn't an end in itself. It should either have an associated view, or, what seems like the best approach in this case, redirect the user to some other page, once it's done sending the email.

Following where you send the email, you might add something like: 
variables.fw.redirect( "product.default" );

Ivan

unread,
Apr 1, 2020, 5:34:19 PM4/1/20
to framework-one
mmhh, I'm sorry but I can't understand.
If I want to call models or beans on my controller, what should I do?

I also tried to do this:


I created in models/beans/mail.cfc 


c
omponent accessors="true" {
 property postmark
;


 
function init() {
 variables
.when = now();
 
}


 
public function sendEmailTest( required string to, required string text, required string subject){
   test
= variables.postmark.sendEmail(
   
From        = "m...@mail.it",
   
To          = arguments.to,
    subject    
= arguments.subject,

   
TextBody    = arguments.text,
   
HtmlBody    = arguments.text
 
);
 
}


}




and in controllers/product.cfc




 
public function show( struct rc ){


   
var mailSend = variables.beanFactory.getBean( "mail" );
   mailSend
.sendEmailTest( to = "info@website", subject ="subject test3", text = "test3" );


 
}
 

but the email is always sent 6 times. I don't understand how to avoid this.

Matthew Clemente

unread,
Apr 1, 2020, 5:38:31 PM4/1/20
to framework-one
The problem appears to be that your controller is getting called by the framework multiple times. I think this is because you're not doing anything after you send the email. What is supposed to happen, after 
mailSend.sendEmailTest( to = "info@website", subject ="subject test3", text = "test3" );
?

Try adding a line to redirect, once you've sent the email. Something like:

 public function show( struct rc ){


   
var mailSend = variables.beanFactory.getBean( "mail" );

   mailSend
.sendEmailTest( to = "info@website", subject ="subject test3", text = "test3" );

   
// once you've sent the email, redirect to a success page
   variables.fw.redirect( "product.default" );

 
}





On Wednesday, April 1, 2020 at 4:32:58 PM UTC-4, Ivan wrote:

Ivan

unread,
Apr 2, 2020, 2:38:01 AM4/2/20
to framework-one
ok, I'm starting to understand ...

But then how can I do such a thing:

in the product page I want to record the page views generated by the visitor in a db table.

i did this simple function in services/product.cfc

public function registerVisitator(required string slug){

query.from( "stats" )
.insert( {
            "stats_date" = Now(),
    "stats_slug" = arguments.slug
} );

}


and in controlles/product.cfc

  public function show( struct rc ){

variables.productService.registerVisitator( rc.slug );

rc.head.title = "page title";
rc.head.description = "lorem ipsum";

}

6 records are inserted in the `stats` table:


stats_id|stats_date           |stats_slug  |
--------|---------------------|------------|
       1|2020-04-02 08:27:20.0|product-page|
       2|2020-04-02 08:27:21.0|images      |
       3|2020-04-02 08:27:21.0|items       |
       4|2020-04-02 08:27:21.0|items       |
       5|2020-04-02 08:27:21.0|items       |
       6|2020-04-02 08:27:21.0|items       |

Nando Breiter

unread,
Apr 2, 2020, 6:01:29 AM4/2/20
to framew...@googlegroups.com
Can you show us the code that is calling product.show and setting rc.slug? Particularly show and explain to us how you set rc.slug.



On 2 Apr 2020, at 08:38, Ivan <ivan....@gmail.com> wrote:


--
FW/1 documentation: http://framework-one.github.io
FW/1 source code: http://github.com/framework-one/fw1
FW/1 chat / support: https://gitter.im/framework-one/fw1
FW/1 mailing list: http://groups.google.com/group/framework-one
---
You received this message because you are subscribed to the Google Groups "framework-one" group.
To unsubscribe from this group and stop receiving emails from it, send an email to framework-on...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/framework-one/ba2a0f66-024c-428e-aed1-4fd8d5e348cf%40googlegroups.com.

Ivan

unread,
Apr 2, 2020, 6:18:18 AM4/2/20
to framework-one
Hi Nando and thanks for the post!

As I wrote above, this is the controllers/product.cfc


 
public function show( struct rc ){

   variables
.productService.registerVisitator( rc.slug );

   rc
.head.title = "page title";
   rc
.head.description = "lorem ipsum";

 
}
 

and is called via GET, like this: website.com/category/subcategory/product


these are the configuration of routes:
 
 routes = [
   
// ...
   
{ "$GET/:category/:subcategory/:slug" = "/product/show/category/:category/subcategory/:subcategory/slug/:slug" }
   
// ....
 
]




To unsubscribe from this group and stop receiving emails from it, send an email to framew...@googlegroups.com.

Nando Breiter

unread,
Apr 2, 2020, 11:57:09 AM4/2/20
to framew...@googlegroups.com
I don’t see where the value of rc.slug is being set in your  code to result in product, images, items, items, items, items being output in tracking record 


On 2 Apr 2020, at 12:18, Ivan <ivan....@gmail.com> wrote:


To unsubscribe from this group and stop receiving emails from it, send an email to framework-on...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/framework-one/d300da62-5d28-43c0-ae28-f6d8c36218cc%40googlegroups.com.

Ivan

unread,
Apr 2, 2020, 12:46:59 PM4/2/20
to framework-one
rc.slug is set in application.cfc to be passed via url:

application.cfc

 routes = [
   
// ...
   
{ "$GET/:category/:subcategory/:slug" = "/product/show/category/:category/subcategory/:subcategory/slug/:slug" }
   
// ....
 
]


controllers/product.cfc:show()
public function show( struct rc ){

 
dump( rc ); abort;
}

result:

slug.png




Il giorno giovedì 2 aprile 2020 17:57:09 UTC+2, Nando ha scritto:
I don’t see where the value of rc.slug is being set in your  code to result in product, images, items, items, items, items being output in tracking record 

Nando Breiter

unread,
Apr 2, 2020, 3:14:55 PM4/2/20
to framew...@googlegroups.com
Ok, so then I don't get how you are generating and hitting these different urls so rapidly. From the stats table you sent before, it seems you've generated 6 urls with varying values for slug in the query string and issued the requests all within a few milliseconds of each other, perhaps with a load testing tool. Is that correct? 
To unsubscribe from this group and stop receiving emails from it, send an email to framework-on...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/framework-one/9a3a6663-e0ed-438f-b5b0-3fc1c1838275%40googlegroups.com.

Ivan

unread,
Apr 2, 2020, 3:43:05 PM4/2/20
to framework-one
no, I'm not doing any "load testing"!

The problem is exactly this: the controller is loaded 6 times!

This is the lifecycle trace:

lifecycle.png

Sean Corfield

unread,
Apr 2, 2020, 4:56:58 PM4/2/20
to framew...@googlegroups.com
You are misunderstanding the trace output. The only call to the `show()` method is "product.show calling item controller". The other five lines are the lifecycle around how the product.show action is queued, how the view is queued and built, and how the layout is queued and built.

To unsubscribe from this group and stop receiving emails from it, send an email to framework-on...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/framework-one/003ea49a-8d8c-4848-8ada-ad9d4a136c6b%40googlegroups.com.


--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles Networks, LLC. -- https://worldsinglesnetworks.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

Sean Corfield

unread,
Apr 2, 2020, 5:06:20 PM4/2/20
to framew...@googlegroups.com
I think the problem might be in your views. Can you show us the contents of your views and layouts files?

That table above suggests that your request for a product page is followed by a request for images and then for four items URLs...

--
FW/1 documentation: http://framework-one.github.io
FW/1 source code: http://github.com/framework-one/fw1
FW/1 chat / support: https://gitter.im/framework-one/fw1
FW/1 mailing list: http://groups.google.com/group/framework-one
---
You received this message because you are subscribed to the Google Groups "framework-one" group.
To unsubscribe from this group and stop receiving emails from it, send an email to framework-on...@googlegroups.com.

Nando Breiter

unread,
Apr 2, 2020, 5:18:12 PM4/2/20
to framew...@googlegroups.com
And I'd suggest using WriteLog() in your product controller show function instead of the registerVisitator() function to make sure the log data is accurate. Something like 

WriteLog( Now() & ' : ' & rc.slug );

If you need help finding the log file this write to, let us know.

public function registerVisitator(required string slug){

query.from( "stats" )
.insert( {
            "stats_date" = Now(),
    "stats_slug" = arguments.slug
} );

}


CarbonZero Sagl
+41 (0)76 303 4477 cell
skype: ariamedia


Ivan

unread,
Apr 3, 2020, 2:38:11 AM4/3/20
to framework-one
omg, with the help of the writelog():

writeLog(text=timeFormat(now(),"hh:mm:ss:l") & " arguments.rc: " & serializejson(arguments.rc), type="information", file="fw1");



in practice, when static resources (images) were requested in the views, the framework tried to solve them.

It was enough for me to modify:

routes = [
  { "$GET/:category/:subcategory/:slug/$" = "/product/show/category/:category/subcategory/:subcategory/slug/:slug/" }
]


Thank you very much everyone for the help!


Il giorno giovedì 2 aprile 2020 23:18:12 UTC+2, Nando ha scritto:
And I'd suggest using WriteLog() in your product controller show function instead of the registerVisitator() function to make sure the log data is accurate. Something like 

WriteLog( Now() & ' : ' & rc.slug );

If you need help finding the log file this write to, let us know.

public function registerVisitator(required string slug){

query.from( "stats" )
.insert( {
            "stats_date" = Now(),
    "stats_slug" = arguments.slug
} );

}


CarbonZero Sagl
+41 (0)76 303 4477 cell
skype: ariamedia


On Thu, Apr 2, 2020 at 11:06 PM Sean Corfield <se...@corfield.org> wrote:
I think the problem might be in your views. Can you show us the contents of your views and layouts files?

That table above suggests that your request for a product page is followed by a request for images and then for four items URLs...

To unsubscribe from this group and stop receiving emails from it, send an email to framew...@googlegroups.com.


--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles Networks, LLC. -- https://worldsinglesnetworks.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

--
FW/1 documentation: http://framework-one.github.io
FW/1 source code: http://github.com/framework-one/fw1
FW/1 chat / support: https://gitter.im/framework-one/fw1
FW/1 mailing list: http://groups.google.com/group/framework-one
---
You received this message because you are subscribed to the Google Groups "framework-one" group.
To unsubscribe from this group and stop receiving emails from it, send an email to framew...@googlegroups.com.

Nando Breiter

unread,
Apr 3, 2020, 9:56:19 AM4/3/20
to framew...@googlegroups.com
Well that was subtle. Well done Ivan. Glad you figured it out. 




To unsubscribe from this group and stop receiving emails from it, send an email to framework-on...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/framework-one/4d1f7f91-2c82-416a-a80b-e6ddd80a1344%40googlegroups.com.

Sean Corfield

unread,
Apr 3, 2020, 9:33:03 PM4/3/20
to framew...@googlegroups.com
I had a feeling it was something about the views and what URLs they were requesting... Glad you figured it out!

To unsubscribe from this group and stop receiving emails from it, send an email to framework-on...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/framework-one/4d1f7f91-2c82-416a-a80b-e6ddd80a1344%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages