I have a function to collect data when I click on the row, I keep the data in the object. When I log the object, it returns object type. But when I try to log its property, it is undefined.
branchToDivision: BranchDto;
onSelect(record: BranchDto) {
this.branchToDivision = record;
console.log(this.branchToDivision);
console.log(this.branchToDivision.brancH_CODE);
}
console screen
GetBranchForView {branch: BranchDto}
undefined
I don't know what problem is.
Here is Object definition
Console display
Does this work for you?
console.log(this.branchToDivision.branch.brancH_CODE);
I find the answer. Each line on the datatable is represented by a class called GetBranchForView. So when I try to catch the event I have to pass a param with type GetBranchForView. This works for me.
onSelect(record: GetBranchForView) {
this.item = record;
this.branchToDivision = record.branch;
}
Related
I am using angularFireStorage (in my Ionic app).
I am having some success, for instance, I can look at my console.log(res) to click the link in my console to view a belt image. I cannot save it to a variable however, which is the issue. Ultimately I would like to use the image tag and link the belt image using src. Here is my code:
const ref = this.storage.ref('belts/');
ref.listAll().subscribe(belt=>
belt.items.forEach(item=> printImage(item))
)
function printImage(imageRef){
let temp = imageRef.getDownloadURL();
temp.then(res=> {
console.log(res)
this.mySRCs.push(res)
})
}
and how I am testing it.
<img [src]="sanitizer.bypassSecurityTrustResourceUrl(mySRCs[0])"/>
and the error:
Uncaught (in promise): TypeError: Cannot read property 'mySRCs' of undefined
TypeError: Cannot read property 'mySRCs' of undefined
I feel like I did something similar a month or two ago but this time I cannot see the image in my app. Thanks for your help.
I think your issue is with using the 'this' keyword within your 'printImage' function.
You either need to do:
belt.items.forEach(item=> printImage.bind(this, item))
or assuming you have a...
const mySRCs = [];
then you just pass the field as an argument to the printImage function...
belt.items.forEach(item=> printImage(item, mySRCs))
and instead of using 'this' inside the function, you use...
function printImage(imageRef, mySRCs){
let temp = imageRef.getDownloadURL();
temp.then(res=> {
console.log(res)
mySRCs.push(res) //<--- remove this
})
}
I have the following class in JavaScript.
class User {
get(id) {
return {id};
}
}
I'm trying to figure out how to get a list of the methods from an instance.
const user = new User();
Basically this means, I'm trying to find code where the input is user and the output is ["get"].
I have tried the following, but they all print [] (which is not what I expect).
console.log(Object.keys(user)); // []
console.log(Object.keys(Object.getPrototypeOf(user))); // []
console.log(Object.getOwnPropertyNames(user)); // []
I know user.get exists because the following line works and prints [Function: get]:
console.log(user.get); // [Function: get]
Obviously this works for properties, and prototype functions. But I'm trying to figure out how to get this to work with class methods. So that means that any solution can not change the User class.
How can I achieve this?
You need to get the prototype of the instance first:
console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(u)));
prints
["constructor", "get"]
I am running into an issue in my Angular 2 app where I'm getting an undefined error that's not making sense to me. What's more perplexing is that, after getting the undefined error, I can effectively check the exact same value in the console and get it with no issues.
First off, here's a version of the function that DOES work:
public getApplicableResult()
{
if (!this.customer || !this.customer.services || !this.customer.services.getAt(0))
{
console.log('Services not available...');
return;
}
else if (this.customer && this.customer.services && this.customer.services.getAt(0))
{
console.log(this.customer.services.getAt(0));
}
When I run this function I get this in the console for my "console.dir" of my object literal:
CustomerServiceDetails
assignments:(...)
authorizations:(...)
If I then click on "assignments" in the console, I get this expanded result:
assignments:
CustomerAssignmentCollection
count:1
items:Array(1)
So all the data is seemingly there and available.
However, if, in my function, if I were to try and access these nested values more directly - such as the value for "count" above, I get an undefined error. That's what I'm not understanding. Perhaps it's something I misunderstand about how the console works. But if seems to me that if I can access the value for this property in the console, then I should be able to access it directly via my function.
Here's an example of the same function, slightly altered to get the result for "count" within assignments, that returns undefined:
public getApplicableResult()
{
if (!this.customer || !this.customer.services || !this.customer.services.getAt(0).assignments)
{
console.log('Services not available...');
return;
}
else if (this.customer && this.customer.services && this.customer.services.getAt(0).assignments)
{
console.dir(this.customer.services.getAt(0).assignments.count);
}
The specific error I get is:
Cannot read property 'assignments' of undefined;
To add some additional info, assignments is not an array, it's a collection.
Two questions here:
1.) Why isn't my first "if" clause handling the 'undefined' error? How could I adjust it to handle the situation better?
2.) Why can I call the first function, get a result, and then drill down from there in the console to get what I need, but if I try and access a more nested part of the object in the function itself, I get undefined as a result?
One additional detail, I was calling this via Angular's ngAfterViewChecked(). So what that should do, even if it's a timing issue, is return the first "if" else clause result until the value is available, and then it should show the result - i.e., it should execute the "else if" block - because AfterViewChecked() keeps checking. But this isn't happening. I'm just getting the undefined result I mentioned above.
I am trying to access the properties of a returned MongoDB (mongoose) find.
If I try to console log the whole object, I can see it all. But if I try to log a property, I get undefined. The object is there!
function getAll () {
let d = q.defer();
User.find({}, function (err, docs) {
if (err) {
d.reject(err);
}
for(let user of docs) {
console.log(user); // This works!
console.log(user.email); // This returns undefined!
}
d.resolve();
});
return d.promise;
}
Any idea?
I also tried to use JSON.parse in case it was stringified (just to make sure) but it wasn't.
UPDATE
So seems like I can access the result using user._doc.email.
But what causes this? I don't remember having to do this before.
If a field in your document shows up when you console.log the whole document, but not when you directly access that field, it means the field is missing in the model's schema definition.
So add email to the schema of User.
PROBLEM: When inheriting an object's properties and methods, a child object seems to loose connection with parent's 'this'. To illustrate it better, look at my example:
function RigidBody() {
this.local = new Matrix4x3();
this.position = new vec3();
...
}
RigidBody.prototype = {
...
setPosition: function(vec) {
this.position.copy(vec);
this.local.setTranslation(this.position);
}
...
};
CameraObject.prototype = new RigidBody();
CameraObject.prototype.constructor = CameraObject;
function CameraObject() {
this.fov = ...
}
CameraObject.prototype.add = ...;
var camera = new CameraObject();
camera.add(...); // Works fine;
camera.setTranslation(...); // Throws "TypeError: Cannot read property 'setTranslation' of undefined
// And on another PC it throws: "http://localhost:8080/js/rigidBody.js(61): Uncaught exception: TypeError: Cannot convert 'this.local' to object"
How to bypass it? I found a workaround for this problem by assigning this.this = this; to parental object and replacing every this by this.this. Unfortunately as a result I need to add .this to every camera function call like this: camera.this.setPosition(...);
as a general advice, please add console.log(camera) to your code and inspect the object in a good browser console, I highly recommend Firebug
that way you can explore what properties and nested properties are available to camera object
from your code example in original question, setTranslation appears to be a property of camera.local, not camera itself.
or perhaps you wanted to call camera.setPosition(...); given that you added it to RigidBody's prototype, but never used it afterwards - jsfiddle?
and given the code provided in comment below:
function Matrix4x3(){
this.element = new Float32Array(16);
}
which does not define setTranslation, so this.local.setTranslation might be undefined as well..
You need to call "super", in other words, inherit the properties from the parent constructor:
function CameraObject() {
RigidBody.call(this); // inherit RigidBody properties
this.fov = ...
}