I have two objects: command and topic.
topic has a method actions that is called from command.
When topic.actions() is called, there are no errors.
When I use the object property command.taxonomy whose value is the string 'topic' to call topic.actions(), an exception is thrown: "Object topic has no method actions".
Why would the console report topic has no method actions when it is right there?
command = {
/*this is set when an html element is clicked by another function:
for this example, it equals the string 'topic'*/
taxonomy : '',
//this is called when the same html element is clicked as above
taxonomy_actions: function(){
this.taxonomy.actions();
}
}
topic = {
actions:function(){
//returns an array of valid commands for topic
this.commands.push('shortcuts');
this.commands.push('action_list');
this.commands['shortcuts'] = new Array();
this.commands['action_list'] = new Array();
for(x in this.aliases){
this.commands.action_list.push(this.aliases[x]);
this.commands.shortcuts.push(x);
}
return this.commands;
}
}
It's actually mostly there in your question:
whose value is the string 'topic' to call ...
What you want is to access the 'topic' property of the Object that contains the topic{} object.
Therefore you want:
taxonomy_actions: function(){
containing_object[this.taxonomy].actions();
}
The containing object (if you didn't attach topic to something) would be window in browser environments or global in NodeJS.
Related
I'm using Mongoose for MongoDB operations in my project. I'm trying to:
find every document matching query
create a new object (let's call it objects)
for every document found create a new object inside objects
save fields from found document to created object
And this works just fine. But I also want to:
for every object saved inside my objects find one document matching query in another collection
if document is found, save fields from it to previously created object objects (as new keys)
My code for the second part looks like this:
for(var i in objects) {
if(objects.hasOwnProperty(i)) {
Model.findOne({name: objects[i].name, id: objects[i].id}, function(e, document) {
if(e) console.error(e);
if(document) {
console.log("Found matchind document"); //This is showed for each object, as expected.
objects[i].newField = document.someData;
objects[i].evenNewerField = document.someMoreData;
}
});
}
}
console.log(objects); //This shows old data from previous operations, no new data from the for loop
Your problem is not your mongoose usage, you should take a deep look to Javascript scope and asynchronous. In your code when console.log is called, objects is not yet updated.
In my cloud code I want to retrieve the first object in the "Messages" class. Then i want to grab some information from that object, send it to another class, and finally delete that object from the "Messages" class i originally pulled it from.
My question is do i need to query the entire "Messages" class just to get the first object in it? I don't want to slow down my app due to inefficient code.
Parse.Cloud.afterSave("sendMessage", function(Parse.Message, response) {
var body = null;
var senderName = null;
var senderId = null;
var randUsers = [];
var query = new.Parse.Query(Parse.Message);
query.find({
success: function(results){
body.push(results[1].get("messageBody"));
senderName.push(results[1].get("senderName"));
senderId.push(results[1].get("senderId"));
response.success(getUsers);
},
error: funtion(error){
response.error("Error");
}
});
});
to avoid confusion: "getUsers" is an arbitrary function call.
To retrieve entry from class, you need the query the table. However, your problem is getting the first record which does not require getting the all record. From your code I can see that you get the first record of result array (result[1] ). You can apply the solution; getting the first record of the class that you want to query. You can do it via two ways; either you can set the limit 1 to your query or you can use the first() method. The Parse.Query JS API link is below;
https://parse.com/docs/js/symbols/Parse.Query.html
Check the limit and first methods.
Hope this helps.
Regards.
Is there a way in javascript to create an object from a string?
Here is an example :
configuation object:
var formatter = {
telephone : {
length : 10,
formatClass : "telephoneFormatter"
},
email : {
length : 255,
formatClass : "emailFormatter"
}
}
In the fields creation I could use the following method :
function createFormatter(element){
if(formatter[element].formatClass == "telephoneFormatter"){
var formatObj = new telephoneFormatter()
}
if(formatter[element].formatClass == "emailFormatter"){
var formatObj = new emailFormatter()
}
return formatObj;
}
But I would like to create a the object dynamically, something like
function createFormatter(element){
return new formatter[element].formatClass();
}
The constructors are not available as properties of the window object, as presented in the solution to "Dynamic Instantiation In JavaScript". The class files are loaded with the page but I cannot find the object developer's tool in chrome. Thus I do not have a handle currently on the classes.
Why do I need to do that ? The application loads form dynamically, and the field are created from an elaborate JSON. While the form is beeing created, the validation is added depending on the structure of the JSON. We never know what validation must be added to a certain field. To complexify the whole thing the validation is different on the locale.
Can that be done?
you can create a custom factory function which checks the configurationObjects formaterClass and than initializes a class of that type. than pass the configurations object as constructor parameter to initialize the object.
register the available classes so the factory function is not a multi condition block.
var formatterClasses = {
'telephoneFormatter': telephoneFormatter,
'emailFormatter': emailFormatter
}
function formatterFactory(configurationObject)
{
return new formatterClasses[configurationObject.formatClass](configurationObject);
}
I'm trying to limit the visibility of some fields of parse User object in cloud function.
I have a "Product" class, with a pointer named "owner" to a the "User" that uploaded the item.
I also have a cloud function called "getProducts", I use query.include("owner") to get the owner data at the same time.
What i want to achieve, is that the output of the "getProduct", will be a list of products, but the "Owner" object will contain only certain fields, such as "firstName" or "facebookId",
I don't want to return to the client other sensitive data even though I'm not presenting it (such as Location, email, family name etc..).
After searching I've seen 2 possible solutions.
1.) Cut the User class into 2 classes, 1 of is "Private" class with ACL just for the user.
2.) The second approach that i prefer, i to edit the fields in the cloud function, but i can't seem to change the "owner" object at the "product" object. i'm getting the error:
"Error: Uncaught Tried to save an object with a pointer to a new, unsaved object. (Code: 141, Version: 1.2.19)"
var output[] = [];
_.each(results, function(result) {
var responseData = {};
var owner = result.get("owner");
//Remove fields from the user object
var itemOwnerId = owner.id;
var itemOwnerFirstName = owner.firstName;
var itemOwnerFacebookID = owner.facebookID;
var itemOwner = new Parse.User();
itemOwner.id = itemOwnerId;
itemOwner.id = itemOwnerId;
itemOwner.firstName = itemOwnerFirstName;
itemOwner.facebookID = itemOwnerFacebookID;
result.set("owner", itemOwner);
responseData.item = result;
output.push(responseData);
});
It seems that calling result.set("owner", itemOwner) isn't good, and throwing me exepction:
rror: Uncaught Tried to save an object with a pointer to a new, unsaved object. (Code: 141, Version: 1.2.19)
What am I doing wrong?
The SDK doesn't allow an object that has been changed to be serialized into a response.
A hack way to work around this would be:
result.dirty = function() { return false; };
This would disable the check and allow you to return the modified object.
If you wanted to re-enable it later, you'd need to store the original value of result.dirty and reassign it later.
all, I want to create a html obejct with javascript in a jsp
page, but 'alert(GameObject1.loginurl);' will alert 'undefined'.
Do I get some misstakes in the code below?
It seems that 'obj.appendChild' has failed.But Why?
var obj;
try {
obj = document.createElement("object");
obj.id = "GameObject1";
obj.name = "JavaGameObject1";
obj.setAttribute("classid", "clsid:72E6F181-D1B0-4C22-B0D7-4A0740EEAEF5");
obj.width = 640;
obj.height = 526;
var loginurl = document.createElement("param");
loginurl.setAttribute("name", "loginurl");
loginurl.setAttribute("value", "xx.xx.xx.xx:8080");
obj.appendChild(loginurl);
document.body.appendChild(obj);
alert(GameObject1.loginurl);
} catch (e) {
alert("Exception:" + e.message);
}
Based on your code, GameObject1 is never defined. I think you are wanting to use obj instead as that is the HTML object for the ID GameObject1.
I will note however that even obj.loginurl will still be undefined as you created the child HTML object param called loginurl, not a property to the HTML object obj. (ie. You would have needed to do obj.loginurl = "xx.xx.xx.xx:8080" to access it the way you seem to want)
To get the child element param's value, you would want something like obj.children[0].value which will return the value you set on your loginurl object. Alternatively, in your current scope you could just call loginurl.value.
When accessing child elements via obj.children[#], it is best to do checks to see if the element at that position exists so you don't throw exceptions everywhere.
To access the loginurl, you could use
alert(obj.getElementsByTagName("param")[0]);
alert(obj.getElementsByTagName("param")[0].name);
alert(obj.getElementsByTagName("param")[0].value);
Accessing like the way you mentioned is proper way of getting the attributes and not the child element.
GameObject1 // Will look for Object variable not the object with id "GameObject1" - You should use document.getElementById("GameObject1")
GameObject1.loginurl // will look for attribute and not the child element