I'm following this example to create an element and call its method. I'm doing:
class streetViewCard extends Polymer.Element {
static get is() {
return 'street-view-card';
}
...
updateLocation(coordinates) {
//empty for now for testing
}
...
customElements.define(streetViewCard.is, streetViewCard);
And then using this element and giving it and id of streetViewCard, then getting this element by:
this.streetViewCard = document.querySelector("streetViewCard");
console.log(streetViewCard); //this logs the element to the console OK, it's NOT null
streetViewCard.updateLocation(feature.geometry.coordinates);
And this gives an error Uncaught TypeError: streetViewCard.updateLocation is not a function. Any idea why?
UPDATE
As requested in the comments, the update from console.log(streetViewCard) is:
class streetViewCard extends Polymer.Element {
static get is() {
return 'street-view-card';
}
static get properties() {
return {
title: {
type: String,
value: 'Street V…
Related
I have the following files:
abstractfoo.ts:
export abstract class AbstractFoo {
public constructor() {
throw new Error("I am not instantiable");
}
public static foo(param: string) {
const str = this.plzOverrideMe();
console.log(str);
console.log(param);
}
protected static plzOverrideMe(): string {
throw new Error("An override of this function is required");
}
};
concretefoo.ts:
import { AbstractFoo } from "./abstractfoo"
class ConcreteFoo extends AbstractFoo {
public static override foo(param: string) {
super.foo(param);
}
protected static override plzOverrideMe() {
return "blah";
}
};
export default ConcreteFoo.foo;
voodoo.ts:
import voodoo from "./concretefoo"
voodoo("whatisthis")
tsc happily compiles these files with no errors. However, it crashes at runtime:
var str = this.plzOverrideMe();
^
TypeError: Cannot read properties of undefined (reading 'plzOverrideMe')
I'm not sure what I'm even looking for. Why is it undefined, and what can I do to get it to return the string i want?
As far as TS is concerned, ConcreteFoo.foo is type (param: string) => void. There is no this parameter, so TypeScript doesn't check that.
If you give your method a this parameter, then it'll error as expected::
public static override foo(this: typeof ConcreteFoo, param: string) {
To make it work at runtime, you'll need to use bind:
export default ConcreteFoo.foo.bind(ConcreteFoo);
which you can read more about at MDN.
some times code says it best. In below example code in Chain.add I have the function name and vars fed in to it. But I am trying to reference the object that the function is associated with. How can I do this
class Chainable {
constructor(...values) {
this._chainableConstruct={
name: this.constructor.name,
values
};
}
}
class Chain {
constructor() {
this.data=[];
}
add(func,vars) {
console.log(func.name); //returns fun
console.log(...vars); //returns test 45
console.log(func.parent); //return undefined want object t from line 28
}
}
class Test extends Chainable {
fun() {
console.log("fun");
}
}
let t=new Test();
let c=new Chain();
c.add(t.fun,["test",45]);
Out of the box, you can't. Furthermore, you can set property values of multiple objects with the same value, so the same function object might have multiple "parents".
I'm using nodejs with typescript. In response on my frontend in addition to status and message of error I want to add one extra property, I have a custom class error handler extending Error type.
export class ApiError extends Error {
status: number;
constructor(status: number, msg: string) {
super(msg);
Error.captureStackTrace(this, this.constructor);
this.status = status;
}
}
i.e. user not found error in frontend's console in data object is following
data: {
message: "User wasn't found", //msg argument in constructor
status: 404, //status argument in constructor
}
I want add to these properties my custom property, how can i do this?
From what I understand you can add additional properties to your custom ApiError class by simply adding it to the constructor
eg: if you want to add a name property you would do the following
export class ApiError extends Error {
status: number;
constructor(status: number, msg: string, name: string) {
super(msg);
Error.captureStackTrace(this, this.constructor);
this.status = status;
this.name = name;
}
}
I'm trying to obtain the constructor arguments for some class but the <> get an array with a undefined element.
For the test I work with services Example2Services and ExampleService
Example2Service.ts
import { ExampleService } from './example.service';
export class Example2Service {
constructor(private es1: ExampleService) {}
getValue() {
return "some value2";
}
}
ExampleService.ts
import { Example2Service } from './example2.service';
export class ExampleService {
constructor(private es2: Example2Service) { }
getValue() {
return this.es2.getValue();
}
getString() {
return "1"
}
}
In this function I send a Example2Service
private resolve(target: Type): Object | Function {
let tokens = Reflect.getMetadata('design:paramtypes', target)
...
}
debugging the code, I get this for the token variable
trying to understand the behavior, I try to obtain the args directly from some classes
import {Example2Service} from '../../exaple2.service.ts';
import {ExampleService} from '../../exaple.service.ts';
private resolve(target: Type): Object | Function {
let tokensA = Reflect.getMetadata('design:paramtypes', Example2Service)
let tokensB = Reflect.getMetadata('design:paramtypes', ExampleService);
let tokens = Reflect.getMetadata('design:paramtypes', target);
...
}
debuging this case I get this, sending a Example2Service to resolve function. Now tokens is an array with an Class inside. Note that when importing the Example2Service and ExampleService this happened, but when changing the import order the behavior changes
EDIT
I found out that this occurs when there is circularity between dependencies
I've started to learn Dart, but I stuck.
I follow this tutorial but something goes wrong.
Here is the problem: I would like to reach out to an API and fetch data from it. Nice!
I've imported packages for doing requests and converting. API returns the data correctly. HTTP GET is working.
The troubles came when I tried to assign json.decode(response.body) to Map().
It always says: The argument type dynamic cannot be assigned to the parameter type Map<String, dynamic>.
Could someone explain why that it's happening and how to handle it?
I'm using Android Studio. I'm invoking the function in the StatefulWidget:
var trends = fetchData('getAll', params);
where params is a Map().
The code:
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
Future<ApiResponse> fetchData(String command, Map params) async {
final String url =
'https://example.com/api/v2/....';
final response = await http.get(url);
if (response.statusCode == 200) {
return ApiResponse.fromJson(json.decode(response.body));
} else {
// If that call was not successful, throw an error.
throw Exception('Failed to load post');
}
}
}
class ApiResponse{
final String result;
final String error;
final String error_number;
final String response_code;
PopnableResponse(
{this.result, this.error, this.error_number, this.response_code});
factory ApiResponse.fromJson(Map<String, dynamic> json) {
return ApiResponse(
result: json['result'] as String,
error: json['error'] as String,
error_number: json['error_number'] as String,
response_code: json['response_code'] as String,
);
}
}
JSON Example:
{
"error":"",
"error_number":"",
"response_code":200,
"result":[
{
"id":1,
"name":"Great Deal",
"day_aired":"2015-07-05 11:06:09",
"trend":"Noone"
},
{
"id":2,
....
}
]
}
Try to cast it
json.decode(response.body) as Map<String, dynamic>