Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Question: How to use querySelector ? Server says querySelector is not a function #637

Closed
haia212 opened this issue Dec 13, 2016 · 6 comments

Comments

@haia212
Copy link

haia212 commented Dec 13, 2016

Hello !

I am trying to use document.querySelector in a service to automatically update all meta tags in a page (it is based on ng2-meta : https://github.com/vinaygopinath/ng2-meta).

document is injected in my service constructor as follow

constructor(@Inject(DOCUMENT) private document: any) 

The problem is that the server keeps displaying an exception

EXCEPTION: this.document.querySelector is not a function

What I am doing wrong ? How to use querySelector properly ?

I am using universal v2.1.0-rc.1. According to this repository, querySelector is supported by universal.

@0radek
Copy link

0radek commented Dec 19, 2016

Correct me if I'm wrong, but don't you need to import isBrowser from ng2-universal, and create a conditional where if that's true, then do your querySelector.

@haia212
Copy link
Author

haia212 commented Dec 20, 2016

Yes it is a solution, except that I am using universal for seo purpose. I would like to render the meta tags of my html on server side. That's why I am using querySelector and I need it on server side (not in a isBrowser if).
According to this issue (#534) querySelector is supposed to be supported, no ?

@0radek
Copy link

0radek commented Dec 20, 2016

Oh ok, my bad! You can try using the meta service as is done here: https://github.com/angular/universal-starter/blob/master/src/angular2-meta.ts

Let me know if that is able to help you. I'll be starting on this probably later part of this week, so it'd be cool to coordinate!

@haia212
Copy link
Author

haia212 commented Dec 20, 2016

Thank you ! It works !

@0radek
Copy link

0radek commented Dec 20, 2016

No problem dude! Good luck.

Also can you close this? :)

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants