Temperamental `nestRemoting()` sometimes can't find relation - javascript

I'm implementing the (not so well documented) loopback method nestRemoting() as follows:
module.exports = function(MyModel) {
MyModel.on('attached', function() {
MyModel.nestRemoting('relationName');
});
}
However this seems to be very temperamental. Sometimes it works, but most of the time I get:
Relation `relationName` does not exist for model `MyModel`
at ....stacktrace....
.....
Does anyone know why this error might be occurring?

So, I found the solution only moments after posting - you can only do nestRemoting after the model has been attatched... not attached. Simple spelling mistake can fix all woes.
Perhaps this post will help someone else trying to nest endpoints.

Related

Is calling a property's method from another method of the same object in this manner ok?

Can someone help me understand why the code below works and if this is an acceptable pattern to use?
Without getting into it too much I have a basic state machine functionality inside an object called states in a simple browser game I am making and I able to trigger event transitions by calling states.event('someEvent') from within the states object itself. For instance if the update method of a given state, say states.someState.actions.onUpdate() is running it can call states.event('someNewEvent'). Doing it this way is not something I need to do, I did it accidentally but I was very confused by the fact that it worked. So the code below is that logic/functionality which I don't understand reduced down to a simple example.
I can't seen to find any information that would explain this type of pattern.
Everywhere online would seem to suggest that I write "this.printText()" instead of "myObj.printText()" but it only prints when I do it the way I have below.
This seems like it might be bad practice, at the very least it would be because I don't understand quite why it works... Any help would be appreciated. I struggle a lot with javascript coming from C# in unity game engine.
let myObj = {
printText: function() {
console.log('I thought this would not print');
},
myProperty: {
toPrint: function () {
myObj.printText();
}
}
}
myObj.myProperty.toPrint();

Bind class instance function to v8::FunctionTemplate

I'm fairly new to C++ and v8 in general, and I wanted to build a native node.js addon, but now I'm stuck on something quite simple IMO, but I can't figure out what the issue is, the error message
C:\Path\To\Project\File.cpp(50): error C2664: 'v8::Local<v8::FunctionTemplate> v8::FunctionTemplate::New(v8::Isolate *,v8::FunctionCallback,v8::Local<v8::Value>,v8::Local<v8::Signature>,int,v8::ConstructorBehavior,v8::SideEffectType)': cannot convert argument 2 from 'v8::Local<v8::Value> (__cdecl *)(const v8::FunctionCallbackInfo<v8::Value> &)' to 'v8::FunctionCallback' [C:\Path\To\Project\build\node_gui.vcxproj]
is not that helpful.
I've got the following code,
v8::Local <v8::Object> Window::GetFunctions() {
v8::Local <v8::Object> DrawFunctions = v8::Object::New(isolate);
v8::Local <v8::FunctionTemplate> bgfnc = v8::FunctionTemplate::New(isolate, &Window::BackgroundCB);
DrawFunctions->Set(v8::String::NewFromUtf8(isolate, "background"), bgfnc);
return DrawFunctions;
}
void Window::Background(const v8::FunctionCallbackInfo <v8::Value> &args) {
v8::Isolate *isolate = args.GetIsolate();
renderer->Background(args[0]->NumberValue(), args[1]->NumberValue(), args[2]->NumberValue());
}
v8::Handle <v8::Value> BackgroundCB(const v8::FunctionCallbackInfo <v8::Value> &args) {
return ((Window*)v8::External::Cast(*(args.Data())->Value())->Background());
}
I want to create an object that contains a list of functions, the functions' callbacks would be member functions of the Window class. I know this has been asked before here, which worked once using a non-member function but otherwise not.
Thanks
Sidenote: I've looked far and wide for v8 docs that are suitable for beginners, the nodesource ones don't explain what the parameters mean or rarely give a thorough example of how to use the function / class, if anyone knows some better docs, that would be great, thank you.
Thanks to the gracious and prompt help of the community, I was able to quickly and effortlessly resolve this issue. Turns out, when writing NodeJS addons, it's a good idea to use NodeJS's own N-API, as the documentation is simpler, clearer, and most importantly, existant.

TypeError: this._url is undefined at Dojo FilteringSelect onChange callback

I keep getting following error: TypeError: this._url is undefined at the callback of a Dojo FilteringSelect.
I simplified my code to this:
var testFeatures = [
{ "id": 1, "Adresa": "Pyšvejcova 6, Kotěhůlky"},
...
];
var filteringSelect = new FilteringSelect({
id: "testSelect",
autoComplete: false,
searchAttr: "Adresa",
store: new Memory({ data: testFeatures }),
onChange: this.onSelected
}, "testSelect");
filteringSelect.startup();
//end of the function, still in the same define callback
onSelected: function () {
//custom processing
var queryTask = new esri.tasks.QueryTask(this.serviceUrl);
var query = new esri.tasks.Query();
query.outFields = ["OBJECTID"];
query.where = "Adresa=" + dojo.byId("testSelect").value;
query.returnGeometry = true;
queryTask.execute(query, this.zoomToSelected);
}
zoomToSelected: function (featureSet) {
//custom map processing
}
and HTML:
<input id="testSelect" data-dojo-attach-point="testSelect" />
I have no idea where's the problem, Google found no case similar to mine. FireBug says the error occurs in init.js. Console says line 199, but the code in question (...nction(){},onExecuteForCountComplete:function(){},onExecuteForExtentComplete:fun...) is on line 256.
One possible cause of problems might be ArcGIS JavaScript API or Web AppBuilder - this seems not to be one of the "ArcGIS-heavy" parts of the code, but I don't know for sure. It's ArcGIS JS API 3.15 (Dojo 1.10) and Web AppBuilder for developers version 1.4.
EDIT: with help of #KenFranqueiro and this post I made some progress. I can't say that I fully understand the onChange callbacks, but I learnt to omit the parentheses while calling a named function. The onSelected still wasn't called, but modifying the input data to include id solved this. But it didn't solve the main problem...
Now the old good TypeError: this._url is undefined occurs at queryTask.execute, or between it and start of zoomToSelected method. The exact place where the error occurs changed to line 256, column 351, with following referenced code:
...e:function(a,b,c,d,f){var m=f.assembly;a=this._encode(n.mixin({},this._url.query...
There is a pointer at the = after "a", so the error seems to be to the right of it, trying to mixin something non-existent to something else. I have no idea what the this in the init.js should mean. Unfortunately, dojo core code is almost undebuggable without some de-obfuscation. Is this my only option, or does anybody know how to solve it?
So debugging the init.js wasn't so hard, it takes QueryTask as this. As far as I know, this shouldn't have a _url attribute. So I must have some mistake in the queryTask as well.
Obsolete part of the question, archived:
I wonder whether the problem might be caused by the onChange callback's argument, which I don't really understand. I've read several examples of Dojo FilteringSelect with onChange event set, like the official one. Usually it has some argument, but I don't know what does it mean and where it gets from. In the official example it corresponds to the div id and nothing else. However, how is the id translated to a variable and even whether my assumption that it's the id is correct. The detailed documentation is not much helpful in this case.
Older EDIT: testSelect is an object. Seems it was created implicitly with the dom - it's a childnode of this.domNode. So now I understand where it comes from, but not how to prepare another function to be called on change and why the correct syntax is what it is.
Also, I don't know where exactly is the problematic undefined this._url supposed to exist. In my example, this refers to the dijit, but I have no idea what is the scope of the part of init.js where the problem occurs.
I'm not sure how much related are the two subquestions (confusion in the onChange callback and identification of this in init.js), I only suppose they are symptoms of the same issue since the error is thrown at the onChange event in my code.
The problem at the query was caused by this.serviceUrl either missing as a part of wrong coontext, or being an object instead of a URL string. So the final step was to make sure the correct context is used (by lang/hitch) and then setting this.serviceUrl to be what I originally thought, one of the attributes of the object referenced as serviceUrl before.

Receive EntryChangedEvent on folder

Currently i'm experementing with the the chrome.fileSystem-Api and i was curious about the new EntryChangedEvent that was added in Version 38. But im writing because i do not really know how to receive this event in my app. I tried it like this, what obviously didn't worked :
chrome.fileSystem.chooseEntry({type: 'openDirectory'}, function(folder) {
if (!folder) {
output.textContent = 'No Folder selected.';
return;
}
folder.on("entrychangedevent",function(v){
console.log(v);
});
});
How do i have to change my code so that i really can use receive EntryChangedEvents?
Thanks!
Link to documentation:
https://developer.chrome.com/apps/fileSystem#type-EntryChangedEvent
I actually think that this feature is not implemented yet. I don't know why it is in the documentation, but if you look at the chromium source there is a function called chrome.fileSystem.observeDirectory (which has a callback that should get these events) but when looking at the implementation it just says:
bool FileSystemObserveDirectoryFunction::RunSync() {
NOTIMPLEMENTED();
error_ = kUnknownIdError;
return false;
}
I found this document which is a request for the API:
https://docs.google.com/document/d/1aW-37JOBZgoD2CIPvoBEgw6bog8gFSowpfqtDEKt5Vo/edit#heading=h.w8inspeo32bj
Anyways, great feature and will probably be available soon.
Three issues here:
It's not clear from the documentation what object this listener should be attached to. In your example, you have attached it to the DirectoryEntry representing a directory. While I don't know the answer, that sounds like it's probably wrong, as a DirectoryEntry is only a means for accessing a directory, and is not itself a directory.
The documentation says that this event is fired for only certain filesystems, and I guess neither of us knows whether it is even effective for whatever file systems we're testing with.
In your example, even if the code were right and the event were enabled, you don't have any changes to the directory, so the event wouldn't fire.

Authenticating via Github (using everyauth)

I'm using everyauth, and am having trouble authenticating it with github. It seems like all of the steps I've followed thus far are correct, but it keeps telling me it cannot GET /auth/github when clicking the "connect with github" link.
Here's a gist with my code: https://gist.github.com/2641260
Any help would be greatly appreciated!
I know its probably too late to answer, but I just had the same problem and figured out the solution.
Its really simple.
Place the EveryAuth code before the app.configure() code.
This way: Fork of your Gist
Not sure if this will help but have you tried adding the ".entryPath" chainable parameter?
everyauth.github
.entryPath('/auth/github')
.appId("APP_ID")
.appSecret("APP_SECRET")
.findOrCreateUser( function (sess, accessToken, accessTokenExtra, ghUser) {
console.log('find user')
// return usersByGhId[ghUser.id] || (usersByGhId[ghUser.id] = addUser('github', ghUser));
})
.redirectPath('/');

Categories