JS Error in Aimeos Lib

Questions around the TYPO3 integration and plugins
Forum rules
Always add your TYPO3, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
tenkraD
Advanced
Posts: 110
Joined: 25 Jul 2017, 08:38

JS Error in Aimeos Lib

Post by tenkraD » 14 Dec 2019, 14:41

Hi Admin,

i am playing with the aimeos jsonapi and run into an error i couldn't fix.
My first attempt was that it has to do with my own extension, but after awhile of frustrating debugging and digging deeper into the code, i tried it on the aimeos demo site and saw that the error also exist there.

How i ran into the js error.
1. Open http://typo3.demo.aimeos.org/demo/detai ... article/0/
2. (Put the demo article into Basket) - Press Button "Add to Basket"
3. (Basket opens, with the demo Article attached)
4. (Add the coupon fixed) - Type "fixed" into coupon field


The error Occurs in the aimeos.js File at the updateBasket function on the second element of basket.included at line 385 when it tries to set the entry.links.self.href.
Entry.links is at the second element of basket.included undefined and therfore you will get bellow JS Error in Browser console. (See Pics Error-1 to Error-3)
aimeos.js?1509296373:385 Uncaught TypeError: Cannot read property 'self' of undefined
at Object.updateBasket (aimeos.js?1509296373:385)
at Object.<anonymous> (aimeos.js?1509296373:337)
at Object.<anonymous> (jquery-2.2.4.min.js:2)
at i (jquery-2.2.4.min.js:2)
at Object.fireWith [as resolveWith] (jquery-2.2.4.min.js:2)
at z (jquery-2.2.4.min.js:4)
at XMLHttpRequest.<anonymous> (jquery-2.2.4.min.js:4)
Attachments
Error-1.jpg
Error-1.jpg (247.26 KiB) Viewed 2420 times
Error-2.jpg
Error-2.jpg (110.95 KiB) Viewed 2420 times
Error-3.jpg
Error-3.jpg (114.23 KiB) Viewed 2420 times
Last edited by tenkraD on 14 Dec 2019, 14:44, edited 1 time in total.

tenkraD
Advanced
Posts: 110
Joined: 25 Jul 2017, 08:38

Re: Uncaught TypeError Aimeos updateBasket / Jsonapi

Post by tenkraD » 14 Dec 2019, 14:42

Pic Step 2 and Step 4
Attachments
Step 2) Demo Article in Basket.jpg
Step 2) Demo Article in Basket.jpg (118.86 KiB) Viewed 2419 times
Step 4) Adding coupon fixed.jpg
Step 4) Adding coupon fixed.jpg (170.54 KiB) Viewed 2419 times

tenkraD
Advanced
Posts: 110
Joined: 25 Jul 2017, 08:38

Re: JS Error in Aimeos Lib

Post by tenkraD » 14 Dec 2019, 14:59

And while playing with the jsonapi i saw something in the template and im a bit confused.
File: aimeos/Resources/Private/Extensions/ai-client-jsonapi/client/jsonapi/templates/basket/standard.php

At line 262 it has this code

Code: Select all

			<?php if( $this->item->getId() === null ) : ?>
				,
				"basket/product": {
					"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'basket', 'id' => $basketId, 'related' => 'product'], [], $config ); ?>",
					"allow": ["POST"]
				},
				"basket/service": {
					"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'basket', 'id' => $basketId, 'related' => 'service'], [], $config ); ?>",
					"allow": ["POST"]
				},
				"basket/address": {
					"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'basket', 'id' => $basketId, 'related' => 'address'], [], $config ); ?>",
					"allow": ["POST"]
				},
				"basket/coupon": {
					"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'basket', 'id' => $basketId, 'related' => 'coupon'], [], $config ); ?>",
					"allow": ["POST"]
				}
			<?php else : ?>
Shouldn't that href and allow be inside self array like this

Code: Select all

			<?php if( $this->item->getId() === null ) : ?>
				,
				"basket/product": {
					"self": {
						"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'basket', 'id' => $basketId, 'related' => 'product'], [], $config ); ?>",
						"allow": ["POST"]
					}
				},
				"basket/service": {
					"self": {
						"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'basket', 'id' => $basketId, 'related' => 'service'], [], $config ); ?>",
						"allow": ["POST"]
					}
				},
				"basket/address": {
					"self": {
						"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'basket', 'id' => $basketId, 'related' => 'address'], [], $config ); ?>",
						"allow": ["POST"]
					}
				},
				"basket/coupon": {
					"self": {
						"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'basket', 'id' => $basketId, 'related' => 'coupon'], [], $config ); ?>",
						"allow": ["POST"]
					}	
				}
			<?php else : ?>


Thanks for clarification
Last edited by tenkraD on 14 Dec 2019, 15:30, edited 2 times in total.

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: JS Error in Aimeos Lib

Post by aimeos » 14 Dec 2019, 15:28

No, the JSON:API response is correct at that place. It must be

Code: Select all

"basket/product": {
	"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'basket', 'id' => $basketId, 'related' => 'product'], [], $config ); ?>",
	"allow": ["POST"]
},
But I think that is the wrong place you are looking for the problem because "entry.links" is already undefined. This is because it's a rebate product you can't change in the basket and therefore, the "links" section is missing:
https://github.com/aimeos/ai-client-jso ... d.php#L109

The the problem is a missing check in Javascript if "entry.links" exists because this isn't always the case.
Can you test that and create PR with a fix?
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

tenkraD
Advanced
Posts: 110
Joined: 25 Jul 2017, 08:38

Re: JS Error in Aimeos Lib

Post by tenkraD » 14 Dec 2019, 15:53

A quick and dirty sollution would be this.
product.data("urldata", csrf);
if( typeof entry.links !== "undefined"){
product.data("url", entry.links.self.href);
}
//product.data("url", entry.links.self.href);

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: JS Error in Aimeos Lib

Post by aimeos » 14 Dec 2019, 16:10

Seems to be a good solution too ;-)
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply