質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

関数

関数(ファンクション・メソッド・サブルーチンとも呼ばれる)は、はプログラムのコードの一部であり、ある特定のタスクを処理するように設計されたものです。

Q&A

解決済

3回答

4889閲覧

is not a functionを解決したい

zzziiii

総合スコア16

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

関数

関数(ファンクション・メソッド・サブルーチンとも呼ばれる)は、はプログラムのコードの一部であり、ある特定のタスクを処理するように設計されたものです。

0グッド

1クリップ

投稿2021/10/24 09:41

TypeError: this.cartNotification.setActiveElement is not a function
を解決したいです。

shopifyのDawnというテーマを元にレイアウトを変えたりしています。
商品ページで、カートに追加というボタンを動かしたいのですが、上記のエラーが出てしまいます。
このエラーはどういうことなのでしょうか?関数でないというのどういうことなのでしょうか?
教えて頂けますと幸いです。

if (!customElements.get('product-form')) { customElements.define('product-form', class ProductForm extends HTMLElement { constructor() { super(); this.form = this.querySelector('form'); this.form.addEventListener('submit', this.onSubmitHandler.bind(this)); this.cartNotification = document.querySelector('cart-notification'); } onSubmitHandler(evt) { evt.preventDefault(); const submitButton = this.querySelector('[type="submit"]'); if (submitButton.classList.contains('loading')) return; this.handleErrorMessage(); /*エラー箇所*/this.cartNotification.setActiveElement(document.activeElement); submitButton.setAttribute('aria-disabled', true); submitButton.classList.add('loading'); const config = fetchConfig('javascript'); config.headers['X-Requested-With'] = 'XMLHttpRequest'; config.body = JSON.stringify({ ...JSON.parse(serializeForm(this.form)), sections: this.cartNotification.getSectionsToRender().map((section) => section.id), sections_url: window.location.pathname }); fetch(`${routes.cart_add_url}`, config) .then((response) => response.json()) .then((response) => { if (response.status) { this.handleErrorMessage(response.description); return; } this.cartNotification.renderContents(response); }) .catch((e) => { console.error(e); }) .finally(() => { submitButton.classList.remove('loading'); submitButton.removeAttribute('aria-disabled'); }); } handleErrorMessage(errorMessage = false) { this.errorMessageWrapper = this.errorMessageWrapper || this.querySelector('.product-form__error-message-wrapper'); this.errorMessage = this.errorMessage || this.errorMessageWrapper.querySelector('.product-form__error-message'); this.errorMessageWrapper.toggleAttribute('hidden', !errorMessage); if (errorMessage) { this.errorMessage.textContent = errorMessage; } } }); }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

int32_t

2021/10/24 12:32

this.cartNotification が何なのか調べましょう。 エラーが出る直前に `console.log(this.cartNotification); console.log(this.cartNotification.constructor.name);` してみるとか。
guest

回答3

0

ベストアンサー

document.querySelector('cart-notification')は正しいですか? document.querySelector('.cart-notification')ではないですか?

もし、document.querySelector('cart-notification')で正しいのであれば、product-formと同様に独自タグをどこかで定義しており、その中でsetActiveElement メソッドを定義しているはずです。

定義が漏れている、依存関係が崩れている、などのことが起きていると思いますので、その部分を確認してみてください。

参考:
Custom elements


ソースコードを見つけたので追記

Dawnテーマのデモページのソースを見てきましたが、やはり独自タグを実装していて、そこで独自メソッドのsetActiveElementを定義しているようです。
このCDNを読み込み忘れている、あるいは読み込み順序を間違えている、などの原因の可能性が高いです。

js

1class CartNotification extends HTMLElement{constructor(){super(),this.notification=document.getElementById("cart-notification"),this.header=document.querySelector("sticky-header"),this.onBodyClick=this.handleBodyClick.bind(this),this.notification.addEventListener("keyup",(t=>"Escape"===t.code&&this.close())),this.querySelectorAll('button[type="button"]').forEach((t=>t.addEventListener("click",this.close.bind(this))))} 2 3/... 4 5setActiveElement(t){this.activeElement=t}} 6customElements.define("cart-notification",CartNotification);

https://cdn.shopify.com/s/files/1/0551/7626/5784/t/31/assets/cart-notification.js?v=1877081553624793631

投稿2021/10/25 01:38

編集2021/10/25 01:57
Lhankor_Mhy

総合スコア36117

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

zzziiii

2021/10/25 02:26

丁寧な回答ありがとうございました! CDNの読み込み順序を変えたら解決しました。 javascriptの記述や定義で何か間違えているor漏れがあるとずっと思い込んでいました。。。
Lhankor_Mhy

2021/10/25 03:19

お役に立てたようで何よりです
guest

0

コンストラクタの

javascript

1this.cartNotification = document.querySelector('cart-notification');

この記述から、「this.cartNotification」はElementという種類のオブジェクトであるとわかりますね。

Element - MDN

で、ここには「setActiveElement」というメソッドが存在しないので、当然エラーになる、と。

投稿2021/10/25 01:18

tkturbo

総合スコア5572

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

zzziiii

2021/10/25 02:32 編集

回答ありがとうございます。 依存関係にある別ファイルがあるのですが、読み込む順がエラーの原因でした。 MDNの記事読んでみます!
guest

0

TypeError: this.cartNotification.setActiveElement is not a function

this.cartNotification.setActiveElement は関数ではない!
とおっしゃってますんで、その記述で間違いないか、スペルミスしてないか、定義に間違いないか、見直してみては

投稿2021/10/24 09:59

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

zzziiii

2021/10/25 02:33 編集

回答ありがとうございます! setActiveElementを定義してあるファイルがあるのですが、それを読み込ませる順番を変えたら解決しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問