Polymer Automatic Node Finding Not Working - javascript

I'm using Polymer Web Components.
Whenever I use this.$ I get an error that it is undefined. I also tried this.shadowRoot and it states that it is null.
For example:
Inside a <polymer-element>:
<script>
Polymer({
cardClick: function(event, detail, sender) {
this.$.ordertemplatediv.style.display = 'none';
// the above line gives the following error:
// "Uncaught TypeError: Cannot read property 'style' of undefined"
this.shadowRoot.querySelector('#ordertemplatediv').style.display = 'none';
// the above line gives the following error:
// "Uncaught TypeError: Cannot read property 'style' of null"
}
});
</script>
Inside another <polymer-element> in another file: (closing tags were omitted for clarity)
<template>
<div id="ordertemplatediv">
<template repeat="{{item in items}}">
<!-- some irrelevant elements -->
Both files' root element is <polymer-element>.
I tried using this.$ many times in different situations in this project but it never worked. Any ideas?
Any help would be appreciated.
EDIT 1 - CONTEXT
This is what I actually want to do:
I have 3 <polymer-elements> which we will call <list-1>, <l-card> and <list-2>.
<list-1> looks something like this: (closing tags were omitted for clarity)
<polymer-element name="list-1">
<template>
<template repeat="{{post in posts}}">
<l-card>
<l-card> contains the <script> above.
<list-2> contains the <template> above.
I want <list-2> to be refreshed (data reloaded) when an <l-card> in <list-2> is clicked (or tapped).

If you're separating the Polymer() call and the <polymer-element> it registers, you'll need to pass the name of the element:
Polymer('your-element', {
...
});
Otherwise, you'll need to put the <template> inside the <polymer-element> so the nameless registration works.

Related

Getting error with vuedraggable on array of objects. View Error nextTick: TypeError: Cannot read properties of undefined (reading '__ob__')

Ive got a page of different charts and wish to drag to reorder them. The details of each chart is stored as an object inside an array. Each chart is a vue child component. These are loaded dynamically in the parent as can be seen below:
<draggable v-model="sections" #change="reorder">
<template v-if="sections.length > 0" v-for="(section, idx) in sections">
<div class="section" :class="[section.class]" v-if="section.active == 't'">
<component :is="section.section"></component>
</div>
</template>
</draggable>
When I drag them to move them I get the following error:
View Error nextTick: TypeError: Cannot read properties of undefined (reading '__ob__')
I think it may have something to do with the page rerendering before the new chart order is stored in the array. Unsure how to resolve this though.
Any assistance appreciated.
I've tried to compute the array order before it render but this has still produced the same result.

How can I pass data from .js to .vue?

I am trying to load data into my .vue component file from its parent .js file.
This is my .html file:
<div class="modal-container">
<section class="modal">
<modal-component></modal-component>
</section>
</div>
This is my .js file:
var modal = new Vue({
el: 'section.modal',
data: {
msg: 'Hello world!'
},
components: {
'modal-component': httpVueLoader('./templates/test.vue')
}
});
This is my .vue file:
<template>
<section>
<p class="test">{{ msg }}</p>
</section>
</template>
However, when I load the page, this error appears in the console and the value of 'msg' is blank:
[Vue warn]: Property or method "msg" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.
There are two different Vue instances in play here. The one you create directly using new Vue has a property called msg. The instance corresponding to the child component, modal-component, does not.
There are 3 main types of properties in Vue. data, computed and props.
If you have a property defined on the parent in data then you can pass it down to the child using a prop.
<modal-component :msg="msg"></modal-component>
In your .vue file you would then need to define msg as a prop.
props: ['msg']
Properties are not automatically inherited, you always need to pass them down using props.
Documentation: https://v2.vuejs.org/v2/guide/components.html#Passing-Data-to-Child-Components-with-Props
Without wishing to muddy the waters, this particular example might be better served using a slot but as it's just an example it's difficult to say for sure whether that would really be appropriate.
Update:
In full the file test.vue would be:
<template>
<section>
<p class="test">{{ msg }}</p>
</section>
</template>
<script>
export default {
name: 'modal-component',
props: ['msg']
}
</script>

React DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node

If state is true to play youtube video, and it is false I would like to delete youtube playing.
MY code is as follows.
{this.state.isPreViewVideo && <PlayYouTube video_id="ScSn235gQx0" />}
sandbox URL:
https://codesandbox.io/s/xryoz10k6o
Reproduction method:
If 4-digit characters are included in input form, "isPreViewVideo: true" by setState and if it is less than false
It works fine when state is true,
but when state is false, I encounter this error as follows.
DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
is there a way to avoid or resolve this error?
In playYouTube.tsx line 78 replace <React.Fragment>...</React.Fragment>
with <div>...</div>
Fragments let you group a list of children without adding extra nodes
to the DOM.
This explains the error
Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
More on fragments here https://reactjs.org/docs/fragments.html
This error, Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node, can also be raised if Google Translate is used (or any other plugin that changes the page DOM).
This is detailed in this Github issue: https://github.com/facebook/react/issues/11538
Problem explanation:
When isPreViewVideo is truthy, you have this:
<PlayYouTube video_id="ScSn235gQx0" />
That probably renders to something like this:
<div> <!-- the root element as rendered by PlayYouTube if isPreViewVideo is truthy -->
<embed /> <!-- the video player or whatever PlayYouTube renders -->
</div>
Now if some code removes the player by directly manipulating the DOM and removing the root div, you'll end up with... Well, with nothing.
But in React's virtual DOM, the root div still exists! So when isPreViewVideo goes falsy, React tries to remove it from the real DOM but since it's already gone, the error is thrown.
The solution:
To expand on #henrik123's answer - wrapping PlayYouTube with a div like this...
<div> <!-- the root element rendered if isPreViewVideo is truthy -->
<PlayYouTube video_id="ScSn235gQx0" />
</div>
...causes this to render:
<div> <!-- the root element rendered if isPreViewVideo is truthy -->
<div> <!-- the element as rendered by PlayYouTube -->
<embed /> <!-- the video player or whatever PlayYouTube renders -->
</div>
</div>
Now the same code that removes the player by removing its root div makes it look like this:
<div> <!-- the root element rendered if isPreViewVideo is truthy -->
</div>
Now when isPreViewVideo goes falsy, the root exists both in React's virtual DOM and in the real DOM so there is no problem in removing it. It's children have changed but React doesn't care in this case - it just needs to remove the root.
Note:
Other HTML elements but div may work too.
Note 2:
Wrapping with React.Fragment instead of a div would not work because React.Fragment doesn't add anything to the real DOM. So with this...
<React.Fragment>
<PlayYouTube video_id="ScSn235gQx0" />
</React.Fragment>
...you still end up with this:
<div> <!-- the root element as rendered by PlayYouTube if isPreViewVideo is truthy -->
<embed /> <!-- the video player or whatever PlayYouTube renders -->
</div>
And you have the same issue.
TL;DR solution:
Wrap PlayYouTube with a div.
This error can occur when you use external JS or jQuery plugins. Let me tell about my case.
Issue description
In my React project I have some links on a page, every link opens a modal with some info and a slider with images inside it. Every link has its own set of images inside the slider. But I have the single modal, it is the same for every link.
Data for modal is stored in a state variable and I change its content on every link click (on every modal open):
const initialData = { title: "", intro: "" }; // here is some empty structure of object properties, we need it for the modalResponse initialization
const [modalData, setModalData] = useState(initialData);
// ...
<div id="MyModal">
<div className="title">{modalData.title}</div>
<div className="intro">{modalData.intro}</div>
<div>{modalData.sliderHtml}</div>
</div>
When I open modal using setModalData() I fill modalData with some new data. HTML-code inside modalData.sliderHtml is something like this structure:
<div class="carousel">
<div class="item"><img src="first.jpg" /></div>
<div class="item"><img src="second.jpg" /></div>
<div class="item"><img src="third.jpg" /></div>
</div>
When modal has opened I call some jQuery code for slider initialization:
useEffect(() => {
$('.carousel').initCarousel({
// some options
});
}, [modalData]);
Then user could close the modal and click the next link. The slider must be filled by new set of images. But I get the error: Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
Error reason
The reason of the issue is in a fact that the jQuery plugin changes the structure of html during initialization. It creates some new blocks inside the .carousel parent (arrows to the left/right, dots for navigation, etc.). And it moves all the .item children inside the new .items block! I get such html structure after the plugin initialization:
<div class="carousel">
<div class="dots">...</div>
<div class="arrows">...</div>
<div class="items">
<div class="item"><img src="first.jpg" /></div>
<div class="item"><img src="second.jpg" /></div>
<div class="item"><img src="third.jpg" /></div>
</div>
</div>
When React tries to change the content of modalData.sliderHtml it executes some magic DOM operations for old structure removing. One of this operations is removeChild method, but the old .item elements can't be found inside the parent .carousel because the plugin moved them inside the new .items element. So, we get the error.
Bug fixing
I started to change modalData content back to the initialData structure every time when the modal is closed.
useEffect(() => {
$('#MyModal').on('hidden.bs.modal', function (e) {
setModalData(initialData);
})
}, []);
So, all html structure inside modalData.sliderHtml is filled by the initial empty data again and we don't get an error on the new link click.
SOLVED for bootstrap alert after manually close button
I had exact error when i close the bootstrap alert error message if it's there manually and then submit the form again. what i had done is to wrap up the AlertError component with extra tag around.
import React from "react";
const AlertError = (props) => {
return (
<div> // <---- Added this
<div className="alert alert-custom alert-notice alert-light-danger fade show" role="alert">
<div className="alert-icon"><i className="flaticon-warning"></i></div>
<div className="alert-text">There is an error in the form..!</div>
<div className="alert-close">
<button type="button" className="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true"><i className="ki ki-close"></i></span>
</button>
</div>
</div>
</div> // <---- Added this
);
};
export default AlertError;
I had same problems with a audio tag and I kept this tag within two nested div and now its working
Most probable cause: something modified the DOM after React
The most probable cause of this issue is that something other than React modified the DOM after React rendered to it.
What's causing this DOM mutation?
Depending on the profile of users who are impacted, there might be a few different causes.
Everyone
If all your users are impacted, you, the developer might be the culprit.
The cause is probably that you are using jQuery or doing DOM manipulations outside of React.
Usual users
If usual users are affected, the most probable cause is Google Chrome translate feature (or Microsoft translate on Edge Chromium).
Thankfully there's an easy solution for that: add a <meta> tag in your HTML <head> (Google documentation).
<meta name="google" content="notranslate" />
This should also disable Microsoft Translator on Edge Chromium.
Advanced users
If only advanced users are affected, and they are not using a translation feature on their browser, then it might be a browser extension or some userscript (most likely run by SpiderMonkey) that causes the problem.
More info
Issue on React repo
SolidJS is also affected
Issue on Chromium bug tracker (please star it to help prioritize it)
This is a very annoying bug to fix. But you should wrap the component that you are rendering it conditionally with span element.
Check this GitHub issue: https://github.com/facebook/react/issues/11538#issuecomment-390386520
In one place of our code was something like this:
render() {
if (el) return <div />
return '';
}
After replacing ' ' with null, all works without errors.
I had the same issue with react. The problem was caused by a google-auth script tag in index.html.
<script src="https://accounts.google.com/gsi/client" async defer></script>
This is while I had another on the JS file causing the issue. Be sure to check for conflicting or repeated CDNs.
In my case, I just needed to reset the state OR make it to initial data when I needed to render it with new data. It fixed the issue.
Ex:
This issue occurred when I was setting table data upon getting API response in useFetch. Before setting the data to state, I first set it to initial value.
setTableData([]);
setTableData(data.response);
I hope it may help someone.
Node.removeChild() - Web APIs | MDN
Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
fix:
unmountContainer() {
// macro task
setTimeout(() => {
// refs
if (containerEle) {
// receive removed node
// eslint-disable-next-line no-unused-vars
let removedChild = document
.querySelector('.page__wrapper')
.removeChild(containerEle);
}
});
}

Angular 2 Uncaught Error: Template parse errors:

Basically this is my code:
<div class="container">
<div [hidden]="loggedIn">
<md-grid-list cols="6" [style.margin-top]="'20px'">
<md-grid-tile [colspan]="1"></md-grid-tile>
And I have already added the md-input-container for instance into my style.css file. When I ran my Angular 2 in terminal it said compile successfully. But when I open my chrome to see the actual website. It does have errors shown below:
Uncaught Error: Template parse errors:
Can't bind to 'colspan' since it isn't a known property of 'md-grid-tile'.
1. If 'md-grid-tile' is an Angular component and it has 'colspan' input, then `verify that
it is part of this module.
2. If 'md-grid-tile' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to
the '#NgModule.schemas' of this component to suppress this message.
You need to use it as, otherwise it will take it as a input property which you need to declare inside the component
<md-grid-tile colspan="1"></md-grid-tile>
When you enter
[colspan]="1"
It expects a variable from the component, what you need is
colspan="1"

removeAttribute is not a function when using angular ng-attr

I'm using ng-attr-data-index={{$index}} in here:
<div class="entry__row" ng-repeat-n="entryRows" ng-attr-data-index={{$index}}>
...
</div>
Now, angular added data-index=0 (and so on) properly, but ng-attr-data-index={{$index}} is still there, and I get this error from console:
TypeError: t.removeAttribute is not a function
Also, this is ng-repeat-n: https://github.com/connorbode/angular-repeat-n

Categories

Resources