ydn-db: calling get() for the second time cause error - ydn-db

I read a value from database, then based on that value, I read a second one. Here is the code:
mydb.db.get("store1", 1)
.then(function(result1) {
// Assume result1 is needed here.
// This second get() will cause error.
return mydb.db.get("store2", 1);
})
.then(function(result2) {
// Assume result2 is needed here.
});
The problem is, the second get() will cause this error (copied from console output in Chrome):
Uncaught TypeError: Cannot call method 'push' of undefined (in deferred.js:397)
If I remove the return clause, no error produced but I cannot get the result2.
I am using ydn.db-isw-core-qry.js (production) v0.8.12 with source map, IndexedDB database on Chrome 33.0.1750.149, Windows.
Is there something wrong with my code? Please help.
Thank you.

Here is test http://dev.yathit.com/test/issue_107.html as you described. No error. Please show your code that cause your error.

Related

Firebase + DialogFlow - Realtime database- Cloud function return query result only after second request

I'm writing a simple Google Action which will read the Firebase Realtime Database, and return result in the response. My problem is, the query result is being passed back in response to DialogFlow only after at least 2 attempts.
Below the screenshots showing the end result in the Simulator
First query screenshot
The first line of the response is returned from the Cloud Function, and contains values passed with the "Context". There is no second line in this response.
below is the screen showing the result after sending exactly the same request second time.
Second query screenshot
First line is the same as previously, but this time I also get the second line which contains the query result data.
It looks like my code is "working" (I get the correct data from the database), but for some reason it only works if I trigger it at least 2 times in quick succession.
Below is the code snipped which handle this request:
function googleAssistantHandler(agent) {
let conv = agent.conv();
let outCommandContext = agent.getContext('outcommand');
let outCharacterContext = agent.getContext('outcharacter');
let character = outCharacterContext.parameters.character;
let command = outCommandContext.parameters.command;
agent.add('<prosody rate="140%" pitch="0.4">' + character +' '+ command +'</prosody>');
var movesRef = admin.database().ref('characters/'+character.toLowerCase()+'/moves/');
movesRef.limitToFirst(1).orderByChild("notation")
.equalTo(command.toString()).on("child_added",function(snapshot){
agent.add(`record number is ` + snapshot.key);
});
}
I've tried using once() instead of on() (as it would make more sense in my case... i don't need to listen to changes on the database, i just want to retrieve data once)- but, I couldn't get it to work.
Can you guys help me out understanding why my query returns result only after the second trigger?
Thanks!
you are using a callback method to get the data from database so there is no guaranty that it will be called before your function is returned. to solve the issue, you need to use a Promise and return that Promise in your function so the last few lines of your function will look like this
return movesRef.limitToFirst(1).orderByChild("notation")
.equalTo(command.toString()).on("child_added").then(snapshot= > {
agent.add(`record number is ` + snapshot.key);
});
You need to always use promises when working with databases. Moreover, the first response that you see might be because of the failed function which timed out. If you see your console logs in firebase, you might see the errors. Also check your default response, if it has the text that User said $name or something similar, then that is what causes the issue in the first attempt.
If you still don't get it to work, try logging the returned data and post your logs here.

Exception in callback of async function: TypeError: callback is not a function

I cant figure out why my insert query code located in server/main.js is causing this: TypeError: callback is not a function error message.
The following code is located at: server/main.js
var businessCard = [{PostedDate: moment().add(0, "days").format('MM/DD/YYYY'), sentBy: currentUserId, recipientName: recipientName }];
Next line is the insert query:
Messages.insert({businessCard: businessCard}, {multi:true});
When I run the code, no inserts into the Messages collection are carried out, neither do I get any error messages in the browser console,
however when I check the terminal I see the following error message:
When I comment out the insert query, the error message disappears, leading me to think there is something wrong in how I have written this insert code.
Kindly help me figure out what am doing wrong here.
Looking forward to your help
The error is because you use multi: true option, insert method does not have this option.

Firebase -- no traceback or line number on error in Chrome

<script src="https://cdn.firebase.com/js/client/2.0.6/firebase.js"></script>
<script>
var root_ref = new Firebase('https://jcatest.firebaseio.com')
function other_function() {
// many lines of code here...
var x = {}
x.x()
// many lines of code here...
}
function my_function(snap){
other_function()
}
root_ref.once('value', my_function)
</script>
If I stick the above code in an html file and open it with Chrome all I see in the console is
"Uncaught TypeError: undefined is not a function ... firebase:26"
In Firefox I see the actual error message:
"TypeError: x.x is not a function ... temp.html:8"
Why am I not seeing the proper error message in Chrome?
Is this a Chrome bug? A Firebase bug? Or am I doing something wrong?
It's a big problem for me because there's a lot of code in my callback and firebase is basically saying, "there's an error somewhere." Not very helpful.
This problem was reproducible on FirebaseJS version 2.0.6. With Firebase 2.2.2, errors and full stacktraces are reported correctly on both browsers.
In the Firebase web client, the Firebase.DataSnapshot object has no method value(), but there is a val() method, which is probably what you're looking for.
See the DataSnapshot docs for more information.
I suspect that the browser is not giving you the relevant error line because the error occurs in an anonymous function, but that's a guess.
If you wrap the callback in a zero ms setTimeout call you get the traceback. Not sure why this is necessary.
root_ref.once('value', function(){ setTimeout(my_function) })

CreateIndexBuffer() crashes,some kind of pointer error.What could be causing this?

I get
Unhandled exception at 0x004687b4 in D3DTest.exe: 0xC0000005: Access violation reading location 0x00000000.
the error is at:
m_d3dDevice->CreateIndexBuffer(sizeof(short)*CHUNK_PRIMITIVES*3,D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_ib, NULL);
Now I checked m_d3dDevice and it's all OK,everything works properly if I don't create the buffer.
m_ib is also propery created before being used in that function:
LPDIRECT3DVERTEXBUFFER9 m_vb;
m_vb = NULL;
I don't think anything else could be causing the problem.I'm confused.
Try enabling the debug runtime from the DirectX control panel that will show you warnings and errors if you have any and always check the return codes from DX functions.
Showing the code for the whole function that creates the index buffer would help too.

Async testing with vows using the http.get library in Node.js

I'm having a doozie of a time trying to get a basic http test to work with vows.
I think I've followed the async example from vows http://vowsjs.org/#-writing-asynchronous-tests and substitued the appropriate calls, but I must be missing something.
The test code looks like this:
var http = require('http'),
vows = require('vows'),
assert = require('assert');
vows.describe("homepage").addBatch({
"Get the home page": {
topic: function() {
http.get({'host': "127.0.0.1", 'port': 5000, 'path': '/'}, this.callback);
},
'should respond with 200 OK': function(res) {
assert.equal(res.statusCode, 200);
}
}
}).export(module);
I get the following error when I try to run the test for this:
/Users/<home_folder>/node_modules/vows/lib/vows.js:80
rrored', { type: 'promise', error: err.stack || err.message || JSON.stringify(
^
TypeError: Converting circular structure to JSON
at Object.stringify (native)
at EventEmitter.<anonymous> (/Users/<home_folder>/node_modules/vows/lib/vows.js:80:90)
at EventEmitter.emit (events.js:64:17)
at /Users/<home_folder>/node_modules/vows/lib/vows/context.js:31:52
at ClientRequest.<anonymous> (/Users/<home_folder>/node_modules/vows/lib/vows/context.js:46:29)
at ClientRequest.g (events.js:143:14)
at ClientRequest.emit (events.js:64:17)
at HTTPParser.onIncoming (http.js:1349:9)
at HTTPParser.onHeadersComplete (http.js:108:31)
at Socket.ondata (http.js:1226:22)
I can get a simple http example to work on it's own. I can get the vows example to work on it's own but I can't combine them for whatever reason. I'd really appreciate some help here. I've been trying to get this to work for a while now (including much googling).
UPDATE:
Apparently adding an error argument to the call back solves this problem, thanks to help from Alexis Sellier (creator of vows).
But I have no idea why. When writing out the http lib example on it's own no error argument is required. I can't find any documentation in vows to indicate why it's needed so I'm at a bit of a loss.
My new question is why is the error argument required when using the http lib in vows?
After checking vow's source code, I think I know why. Vows always ensure that when you call this.callback, the resulting receiver function's first argument is always an error object. Vows interpret the callbacks by these rules:
If the first argument of your originating callback is a boolean, use that to determine whether or not to append an error object to the receiving callback (e.g. path.exists(boolean) will emit callback(error, exists) instead)
If the first argument is an object, assume it's an error object and use that to determine whether to add the originating callback to the "error" or "success" list. The reason this list exists is to support promise based tests I guess?
While I can't confirm the above is correct, my experience is that vows' async style is made to support node-styled callbacks (e.g. err as the first arg), and 3rd party npm modules that don't conform to this standard will be hard to test.
Please don't take my answer as gospel, as this is my own experience. Another gotcha is when you have async operations inside the function that you want to test - unless you provide a callback, vows won't be able to handle it properly.
Personally, I think vows still make it hard to test async code. I wish it had some waitFor() or until() flow control functions though.
My suggestion? When dealing with async code, use Step. Don't let vows control your flow.
It is actually missing in the documentations which is still a bit short. But you can get a glimpse of it here in this page :
'when peeled *asynchronously*': {
topic: function (banana) {
banana.peel(this.callback);
},
'results in a `PeeledBanana`': function (err, result) {
assert.instanceOf (result, PeeledBanana);
}
}
As it was said by Morten Siebuhr and Ruben Tan, this is how vows works and that is why it works like that.

Resources