Hello I want to build a local database for my phonegap so user can use it offline.
I have this in angular function that creates a database.
function Database() {
return {
create: function (itemDocs) {
var db = null;
var request = indexedDB.open("myDB", 1);
request.onsuccess = function (event) {
db = event.target.result;
console.log("DB loaded successfully");
};
request.onerror = function (event) {
console.log(event)
};
request.onupgradeneeded = function (event) {
db = event.target.result;
console.log("DB initiliazed / created");
//create collections
db.createObjectStore("items", {keyPath: "_id"});
//create documents
var transaction = db.transaction(["items"], "readwrite");
var items = transaction.objectStore("items");
items.add(itemDocs);
};
}
}
}
The itemDocs holds a mongoDB collection (which is an array of objects) and I want to store that collection inside indexedDB database the problem im having is that I'm getting this annoying error.
Uncaught InvalidStateError: Failed to execute 'transaction' on 'IDBDatabase': A version change transaction is running.
Use var transaction = event.target.transaction instead of var transaction = db.transaction(...);
A full answer is rather lengthy. Briefly, you don't want to create a new transaction in onupgradeneeded. There is already an active transaction available for you.
Related
i am new in indexedddb.
when I want to create a transaction with a inceased version of the database (I increase the version because otherwise the upgradneeded event is never executed), I have the error "A version change transaction is running" which displays.
that's my code (it's from MDN):
var request = window.indexedDB.open("new-db", 8);
request.addEventListener('upgradeneeded', event => {
console.log("bonjour");
var db = event.target.result;
var request = db.transaction([], "readwrite")
.objectStore("customers")
.delete("444-44-4444");
request.onsuccess = function (event) {
// c'est supprimé !
};
});
request.onsuccess = function () {
console.log("open");
}
THANKS FOR YOUR HELP.
Replace this:
var request = db.transaction([], "readwrite")
.objectStore("customers")
.delete("444-44-4444");
With this:
var existingVersionChangeTransaction = event.target.transaction;
existingVersionChangeTransaction.objectStore('customers').delete('444-44-4444');
Node.js command prompt is simply ignoring this function, while the other functions are getting deployed, I am not getting any error either.
var database = admin.database();
var postsRef = database.ref("/posts");
postsRef.on('child_added', addOrUpdateIndexRecord);
function addOrUpdateIndexRecord(dataSnapshot) {
// Get Firebase object
var firebaseObject = dataSnapshot.val();
// Specify Algolia's objectID using the Firebase object key
firebaseObject.objectID = dataSnapshot.key;
// Add or update object
index.saveObject(firebaseObject, function(err, content) {
if (err) {
throw err;
}
console.log('Firebase object indexed in Algolia', firebaseObject.objectID);
});
}
If it's a database trigger function, then the syntax you are using is not correct. Try doing this way:
exports.updateposts = functions.database.ref("posts/")
.onWrite(event => {
//Do whatever you want to do with trigger
});
Here i am sending sensor data to database using simple javascript (johnny five)
using this code
var five = require("johnny-five");
var pg = require("pg");
var conString = "pg://admin:raja#localhost:5432/data";
var client = new pg.Client(conString);
function call(n)
{
client.connect();
client.query("INSERT INTO sensordata (event) VALUES (n);");
}
var board = new five.Board();
board.on("ready", function() {
var sensor = new five.Sensor.Digital(2);
sensor.on("change", function() {
var n = this.value;
call(n);
});
});
My question is can i pass the streaming data variable n in the sensor call back function to call method.
I think, what you're looking here is a simple query templating, supported by all sql clients:
client.query("INSERT INTO sensordata (event) VALUES ($1);", n);
This way you'll be able to pass the value of n variable to your SQL query.
Update: And you don't have to call client.connect() before every call to client.query. You should only call it once to establish DB connection, after that pg.Client will use it to send all subsequent requests.
I have been following the page Using IndexedDB.
When the page is loaded, I create/load the database.
window.App = window.App || {};
App.db = window.indexedDB.open("myapp", 1);
Following these, I define onerror, onupgradeneeded and onsuccess for App.db object.
In onupgradeneeded, I create a new object store.
Finally, the onsuccess method is called automatically by Javascript because the database is created/loaded. In this method, I list the details of App.db.
App.db.onsuccess = function(ev){
console.dir( App.db );
};
Console shows that,
readyState = "done" result = IDBDatabase transaction = null
I called the page with file:///, http://localhost, http://myownserver.that.is.forwarded.to.127.0.0.1.from.domain.provider separately.
but the problem persists. The transaction method is null, and I can't do anything, start any transaction. What is the problem, what am I missing?
indexedDB.open returns a request object (IDBRequest). So your App.db will hold the request.
It sounds like you have a handle on the upgrade process.
Once that's complete, the connection object (an IDBDatabase instance) is returned via the result property of the request, and you use the transaction() method on the connection object to start transactions. So you'd want to write your "success" handler more like:
App.db.onsuccess = function(ev){
var connection = App.db.result;
var tx = connection.transaction(stores, mode);
...
};
But it's more common to call the connection object something like db and you don't need to hold on to the request once the connection is opened, so what you probably meant to do is something more like this:
var openRequest = indexedDB.open("myapp", 1);
openRequest.onupgradeneeded = function(e) {
var db = openRequest.result;
db.createObjectStore(...);
...
};
openRequest.onsuccess = function(e) {
App.db = openRequest.result;
var tx = App.db.transaction(...);
};
I have this nodejs app which fetches feeds from some website.
The approach to fetch is through an event emitter since its connecting to a streaming API.
Anyway, the fetching works because I get the response log continuously from this:
console.log(JSON.stringify(feeds));
However it seems that storing the result to the database does not work. Although the store_feeds works.
I am not sure if the problem is with NodeJS or MongoDB, because if I create a loop to manually input JSON object to store_feeds, I can see that the db is updated.
Code for the event loop:
// Worker
var worker = setInterval(function(){
var q = "some query";
Feed.EventEmitter.once("feeds", function(feeds){
console.log(JSON.stringify(feeds));
store_feeds(JSON.stringify(feeds));
});
get_feeds(q);
console.log("\nWorker is running...\n");
}, 5000);
This is the code for storing feeds to MongoDB:
// Store
function store_feeds(feeds) {
var mongodb = require('mongodb');
var server = new mongodb.Server("IP_ADDRESS", 27017, {});
new mongodb.Db('test', server, {}).open(function (error, client) {
if (error) throw error;
var collection = new mongodb.Collection(client, 'feeds');
collection.insert(feeds, {safe:true},
function(err, objects) {
console.log("\nStoring Feed Results: " + feeds + "\n");
if (err) console.warn(err.message);
if (err && err.message.indexOf('E11000 ') !== -1) {
// this _id was already inserted in the database
}
});
});
}
This is code for the Event Emitter:
// Event Emitter
var Feed = (function(){
var eventEmitter = new events.EventEmitter();
return {
EventEmitter : eventEmitter, // The event broadcaster
latestFeed : 0 // The ID of the latest searched feed
};
})();