How to get valid result from table.get using Dexie/IndexDB - javascript

I'm a PHP coder so not fully in the know with Javascript so possibly making a real schoolboy error here. I'm trying to facilitate some client side DB using Dexie & IndexDB. I can get values in using db.table.put and retrieve them all using db.table.toArray. However I seem to be struggling with getting individual records back out again. I'm trying to query for Keydata based on a form input. Heres my code, but it returns a TypeError cannot read property of undefined.
Here I can confirm in line 4 that my 'search2' variable contacts keyData (e.g. 1). But when used in line 5 it doesn't return a valid result. Can anyone see the issue?
function searchData(){
var search2;
search2 = document.getElementById('search2').value;
alert ("Id " + search2);
db.contacts.get(search2).then
(function (contact) {
alert ("Id " + contact.id + " : " + contact.title + " " + contact.forename + " " + contact.surname);
}).catch(function(error) {
alert ("Ooops: " + error);
});
}

Related

How to get data from a especif Time period in InfluxDB Node-RED

I'm trying to read data from InfluxDB v1.x but only in a especific time period, Example:
Get the values of temper01 from 2021-11-09/14:00:00 to 2021-11-10/14:00:00.
i know its possible with this code :
"WHERE time >= '2021-11-09T14:00:00Z' AND time <= '2021-11-10T14:00:00Z'" But i wanna know if it is possible to use a variable instead of the direct timestamp.
i've tried this code :
msg.query = "SELECT time , Temper01, Temper02, Temper03 FROM "
+ global.get("ID_device1")
+ " WHERE time >= "
+ msg.payload;
+ " AND time <= '2021-11-18T00:00:00.000Z' " ;
but dosent matter the msg.payload value, it just get data from the very first data stored, and when i try to single quote msg.payload i get the invalid operation: time and *influxql.VarRef are not compatible" error message.
Assuming that msg.paylaod looks something like 2020-11-18T00:00:00.000Z then you have not quoted the time string you are inserting into the query.
It should look like this:
msg.query = "SELECT time , Temper01, Temper02, Temper03 FROM "
+ global.get("ID_device1")
+ " WHERE time >= '"
+ msg.payload;
+ "' AND time <= '2021-11-18T00:00:00.000Z' " ;
The difference is the ' at the end of the string on the 3rd line and again at the start of the string on the last line.
Directly comparing a working version with the version that didn't work would have shown this.

Javascript .indexof 'typeError' error despite forcing string conversion

JS drives me insane with issues like this. I have the following code which creates a string (composed of session data and date information) to be written to an array, as such:
var _writes = String(req.session.subscriber + ":" + req.session.postal + "[" + req.session.id + "]=" + _onYear + "-" + _onMonth + "-" + _onDay + "-" + _onHour + "-" + _onMinute);
_users.push(_writes);
Later, I wish to perform an 'indexof' command on the string of the array, as such:
for (_cycle = 0; _cycle < _users.length; ++_cycle) {
_seeks = String(_users[_cycle]);
_score = _seeks.indexof("="); //ERROR THROWN HERE
//do other stuff here...
} //for loop
My error is "TypeError: _seeks.indexof is not a function"...? I thought by converting everything to a string I should be able to perform the 'indexof' command. Can somebody please advise what the issue is here? I thank you in advance.
Probably not a js issue. You are using "indexof" instead of "indexOf" (Uppercase O). Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
It should be:
_seeks.indexOf("=");
Don't give up, it will make sense soon :)

How do I reference a value in an App Maker query (like I do in Apps Script)

I am writing a script to take a stock number, loop through existing stock numbers until a match is NOT found, then assign that unique stock number to the record. My problem is that the usual data[i][2] doesn't seem to reference a 'query' the same way that Apps Script would reference an array.
Fair warning, I'm trying to expand my Apps Script skills in to broader Javascript so I there's a good chance I'm doing it all wrong - I'm all ears if you tell me I'm doing this all incorrectly!
Using the log: data[i][2] gives me 'undefined' whereas data[2] gives me all fields of the third item in my query. Based on this I feel like I just need to learn how to reference it properly.
//Querying my datasource as 'var data'
var query = app.models.UsedVehicles.newQuery();
query.filters.ParentDealType._contains = prefix;
var data = query.run();
//Returns four records which is correct.
var testStockNo = prefix+month+countstring+year;
console.log("Test Stock Number " + j + ": " + testStockNo);
for (i = 0; i < data.length; i++){
console.log("data[i][2]: " + data[i][2]); //results: undefined
console.log("data[2]: " + data[2]); //results: all fields of 3rd query result.
if(data[i][2] === testStockNo){
k++;
break;
}else{
console.log("No Match");
}
}
Even if testStockNo equals the value in field:TStockNo, the log displays:
Test Stock Number 1: C1200118
data[i][2]: undefined
data[2]: Record : { TIndex: 8, TVin8: HS654987, TStockNo: null,
TParentStkNo: GSD6578, TYear: 2010, TMake: NISSAN, TModel: PICKUP,
TMileage: 24356, ParentDealType: C}
No Match
Issue/Solution:
query.run() returns array of records and NOT a array of arrays(2D). You should access the Record value using it's key instead of a index.
Snippets:
console.log("data[i][TStockNo]: " + data[i]['TStockNo']);
console.log("data[i].TStockNo: " + data[i].TStockNo);
console.log("data[2]: " + data[2]);
References:
Query#Run

Can't access (only) some of an object's properties (JavaScript)

I'm using firebase / AngularJS, but I think this is a plain JavaScript object structure problem in my opinion.
Signing in Facebook manually with firebase, I cannot get access to 4 specific properties that are inside the "result" object from the signInWithCredential process.
(console) Logging the "result" object, the 4 variables are in there indeed, I just cannot get direct access to them.
However, I can get access to all the other properties of the object, which turns this into a really weird bug.
Here is the code :
firebase.auth().signInWithCredential(credential).then(function(result){
// $localStorage.firebaseToken = result.providerData.stsTokenManager.accessToken;
var chatUserData = {};
var updates = {};
console.log("result str obj : " + JSON.stringify(result)); // defined
console.log("result str > uid is " + JSON.stringify(result.uid)); // defined
console.log("result str > displayName is " + JSON.stringify(result.displayName)); // defined
console.log("result str > photoURL is " + JSON.stringify(result.photoURL)); // defined
console.log("result str > email is " + JSON.stringify(result.email)); // defined
console.log("result str > emailVerified is " + JSON.stringify(result.emailVerified)); // defined
console.log("result str > isAnonymous is " + JSON.stringify(result.isAnonymous)); // defined
console.log("result str Provider Data " + JSON.stringify(result.providerData)); // array of objects - defined
console.log("result str > apiKey is " + JSON.stringify(result.apiKey)); // gets undefined
console.log("result str > appName is " + JSON.stringify(result.appName)); // gets undefined
console.log("result str > authDomain is " + JSON.stringify(result.authDomain)); // gets undefined
console.log("result str > stsTokenManager is " + JSON.stringify(result.stsTokenManager)); // gets undefined
chatUserData.username = result.displayName;
chatUserData.email = $localStorage.fb_data.data.email;
chatUserData.id = result.uid;
updates['/users/' + result.uid + "/"] = chatUserData;
firebase.database().ref().update(updates);
}).catch(function(error) {
console.log("Error! " + JSON.stringify(error));
});
And here is the object code from the console log :
which makes this into a weird bug because I know the object is there, and supposedly I'm accessing it right, but it just doesn't load. This is usually just a stupid mistake I made, but I just can't seem to find it.
(the facebook profile I have there is fake)
Hope you guys can help, Cheers.
I fixed this, turns out this is a kind of firebase-managed object, the way to correctly loop through it is using angular.foreach. And even that way is not the correct one.
I should have used user.getToken(), and I found it right in the documentation.
Firebase User Get Token

Returning a concatenated string from a functions' properties in javascript

I'm a total JS beginner. Here is the JsBin link formLetter test should be passing.
TL;DR
This:
var formLetter = function(recipient, msg, sender) {
return "Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender
};
console.log(formLetter("Hermione", "Luna","How are you?"));
Should return:
"Hello Hermione,
How are you?
Sincerely,
Luna"
But instead I get this:
"Hello [object Object],
undefined
Sincerely,
undefined"
Edit
Sorry for the confusion. I'm working on different problems inside one JsBin. This is the correct JsBin with the isolated code.
This is because you are only getting one object passed into the function call. This object contains the information you need in lieu of the named arugments you have provided.
The first argument, recipient being [object Object] tells you that it's an object. undefined means that nothing was passed in their place. This signifies the common pattern of a config or param object being passed to the function call. Because of this, what you have as named arguments should really be property look ups on the object provided as the first argument.
Your function definition should look more like:
var formLetter = function (letter) {
// do something with letter
};
Inside of that function call, you may then hit the properties of the letter object to see if they contain what you need, Doing console.log debugging in dev tools will help track it down.
The line:
var formLetter = function(recipient, msg, sender) {
return "Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender
};
in your example needs one semicolon after "sender", like:
var formLetter = function(recipient, msg, sender) {
return "Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender;
};
Your undefined is related to the use of anidated console.log.
You do:
console.log(longMessage.formLetter("Hermione", "Luna","How are you?"));
and (in the JsBin) you have also:
var longMessage = {
formLetter: function(recipient, sender, msg) {
console.log("Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender);
}
};
In the example of your question you have them corrected.
Double check the code you post, please.
After looking at your test in jsbin, I noticed that in your assert.deepEqual() method you run formLetter(letter) and compares it to the concatenated string you created.
The problem is that formLetter() expects three string values and you send it an object (letter). That's why you get [Object object] in the first location and undefined in the others.
You should run formLetter(letter.recipient, letter.msg, letter.sender) in your assert and it should work properly.

Categories

Resources