Trying to build a Metro app using Javascript and having issues with IndexedDb. I cannot create an object store. My code is shown below. I'm doing this on success of the open() function.
dbReq.onsuccess = function (evt) {
var txn = evt.target.transaction;
var db = evt.target.result;
if (!db.objectStoreNames.contains("test")) {
var store = db.createObjectStore("test");
}
}
Every time, it throws an exception on the 'createObjectStore' call that says
0x800a139e - JavaScript runtime error: [object IDBDatabaseException]
Over here they talk about it and it's a nice example to look at too, but still, did not help me.
Notice that control hits the one line of code inside 'if' statement. So 'db' is not null and is valid. But I saw that the transaction is null - not sure if that is an issue or even if you are supposed to get a valid transaction back at this point.
Not sure why it was not working. Switched to using roaming settings and it is very easy to use.
roamingSettings.values[SETTING_NAME] = SETTING_VALUE;
To read, of course,
var temp = roamingSettings.values[SETTING_NAME];
Related
This question already has an answer here:
Assignment to constant variable exception
(1 answer)
Closed 1 year ago.
Im attempting to load a TensorFlow model into a javascript file and I'm getting an uncaught TypeError from a line but cant understand why I'm getting that error.
this is the block of code from which the error comes
const demosSection = document.getElementById('demos');
const MODEL_FILE_URL = 'models/Graph/model.json';
// For Keras use tf.loadLayersModel().
const model = tf.loadGraphModel(MODEL_FILE_URL);
// Before we can use COCO-SSD class we must wait for it to finish
// loading. Machine Learning models can be large and take a moment to
// get everything needed to run.
model.then(function(loadedModel) {
MODEL_FILE_URL = loadedModel;
// Show demo section now model is ready to use.
demosSection.classList.remove('invisible');
});
and this was the console error message - letsscan1.js:12 Uncaught (in promise) TypeError: Assignment to constant variable. at letsscan1.js:12
const type variables are of final type. Once this variable is assigned a value, its value cannot be changed.
You are overriding it here.
const demosSection = document.getElementById('demos');
let MODEL_FILE_URL = 'models/Graph/model.json'; // <-- this variable gets assigned another value below. Changed it to let
// For Keras use tf.loadLayersModel().
const model = tf.loadGraphModel(MODEL_FILE_URL);
// Before we can use COCO-SSD class we must wait for it to finish
// loading. Machine Learning models can be large and take a moment to
// get everything needed to run.
model.then(function(loadedModel) {
MODEL_FILE_URL = loadedModel; // <-- you were reassigning a value to a const variable here.
// Show demo section now model is ready to use.
demosSection.classList.remove('invisible');
});
First of all, yes I know this question has been asked before, but I still cannot figure out how to make it work. I believe the problem is, I am running files individually through node.js on my Mac terminal, sorta like applications.
Here is the deal. I have one file, bitt1.js, that has var mid = 293.03;.
In my other file, otherFile.js, I have an if, else statement, depending on the variable mid (which is in bitt1.js):
if (mid <= 290) {
trade = true;
} else {
trade = false; }
The issue is, in terminal, I run first bitt1.js, then after I run otherFile.js. This makes it so I can't receive the mid variable from bitt1.js and it comes up as undefined.
How can I solve this issue? I've only found things used within html or etc, where the variables are always "available".
I'm new to JS and this whole thing so some of the stuff I said may be incorrect... and I could have also just been being dumb and the answer is obvious, but please help me out... I've thought about creating a JSON file and writing/reading data from it using the two other files, but I feel there's a better way...
Thanks!
Developer NodeJS's code works if you don't want to modify the value of the variable - if you just want to share the initial value of the variable, it works perfectly.
But if you intend to mutate the value of mid during runtime execution of bitt1.js and want to use that value, perhaps you can use a Unix pipe to plug its value into the stdin of bitt1.js.
E.g.
// bitt1.js
var mid = 299;
console.log("<mid>%d</mid>", mid); // this is piped to stdin
// otherFile.js
var stdin = process.openStdin();
var data = "";
stdin.on('data', function(chunk) {
data += chunk;
});
stdin.on('end', function() {
data.match(/<mid>([\s\S]+)<\/mid>/i);
var mid = +data.match[1];
console.log(mid);
});
Then running: node bitt1.js | node otherFile.js
Would print 299 from within otherFile.js.
This is a rough solution though: it should require some undefined checking on the match expression, and of course piping doesn't allow you to print anything directly to console in the bitt1.js file - you'd have to reprint everything in otherFile.js, which leads to duplicate code.
But it could be a solution that works for you, all depends on your requirements! Hope this helps.
node.js allows imports and exports.
Say bitt1.js has:
var mid = 299
console.log(mid)
// Here is where you export the desired value
//export default mid
module.exports.mid = mid
Then, in your otherFile.js
// you import the value from bitt1.js
var mid = require('./bitt1')
console.log(mid) //Outputs 299
That's it.
Edit: updated answer
I have an ionic app that uses the set data structure in one of it's method. When I try to run the app on my android device(Android 5.0.2,API 21), I run into this error
ReferenceError: Set is not defined
at Object.myMethod
Here is a code snippet showing the line responsible for the error
myMethod: function(userid) {
var user = [];
var service = this;
var userIDs = new Set();
var promises = [];
...}
I am not a javascript guru but it seems to me that it might be a problem with the android web view on my device not having a built in implementation of the Set data structure. To further confuse matters, I tested this same app on another device(HTC One M8(Android 5.0.1,API 21)) and it worked fine with no errors shown. Does anybody know how to fix this?
You should try a polyfill for those devices that does not support Set:
https://github.com/medikoo/es6-set
It's not the most optimal option, but you could use dictionary with boolean values...
var userIDs = {};
userID["anyUserId"] = true;
I noticed that if I execute a JavaScript script using the mongo command, the script can treat a cursor object as if it was an array.
var conn = new Mongo('localhost:27017');
var db = conn.getDB('learn');
db.test.remove({});
db.test.insert({foo: 'bar'});
var cur = db.test.find();
print(cur[0].foo); //prints: bar
print(cur[1]); // prints: undefined
This seems like it should be beyond the capabilities of the JavaScript language, since there is no way to "overload the subscript operator". So how does this actually work?
As documentation says, it is special ability of driver. It automagicly converts cursor[0] to cursor.toArray()[0]. You can prove it by overriding toArray() with print function or new Error().stack to get callstack back. Here it is:
at DBQuery.a.toArray ((shell):1:32)
at DBQuery.arrayAccess (src/mongo/shell/query.js:290:17)
at (shell):1:2
As you can see, indexing calls arrayAccess. How? Here we have a dbQueryIndexAccess function, which calls arrayAccess.
v8::Handle<v8::Value> arrayAccess = info.This()->GetPrototype()->ToObject()->Get(
v8::String::New("arrayAccess"));
...
v8::Handle<v8::Function> f = arrayAccess.As<v8::Function>();
...
return f->Call(info.This(), 1, argv);
And here we have a code, which sets indexed property handler to this function. WOW, v8 API gives us ability to add this handler!
DBQueryFT()->InstanceTemplate()->SetIndexedPropertyHandler(dbQueryIndexAccess);
... and injects it into JS cursor class, which is defined originaly in JS.
injectV8Function("DBQuery", DBQueryFT(), _global);
Tl;dr: It is hacked in C++ source code of mongo shell.
I'm using following code in JScript (WSH) to connect to local registry using WMI: var registry = GetObject('winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\default:StdRegProv'); and that works.
Then I have to determine if I'm allowed to delete key without really trying to delete it (e.g. perform a non-destructive check). I looked over docs and found that I need StdRegProv.CheckAccess() method. Problem is that CheckAccess returns result as out argument and I could not find VBScript's ByRef equivalent in JScript.
Somewhere in the Internet I've found that using SWbemServices.ExecMethod would help somehow, but I hadn't figured out how can I use that yet.
Could anyone provide me with code sample in JScript performing function call with argument passed by reference?
Heh, got it working.
For anyone who will need it, CheckAccess invokation in JScript looks something like this:
function CheckAccess(defKey, subkeyName, required) {
var providerName = "StdRegProv";
var funcName = "CheckAccess";
// connect to WMI
var services = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\default");
// get provider
var registry = services.Get(providerName);
var in_params = registry.Methods_(funcName).InParameters.SpawnInstance_();
in_params.hDefKey = defKey;
in_params.sSubKeyName = subkeyName;
in_params.uRequired = required;
var outParams = services.ExecMethod(providerName, funcName, inParams);
return Boolean(outParams.bGranted);
};