The on `data` or `error` is never fired on createKeyStream level - leveldb

I have the following code to return all the keys in the level DB in Node.js app, this is a method in a ES6 class:
class LevelDB {
constructor() {
this.db = level(chainDB);
}
getAllItems() {
let self = this;
return new Promise(function(resolve, reject){
self.db.createKeyStream()
.on('data', function (data) {
resolve(data);
})
.on('error', function (err) {
reject(err)
});
}
}
The database is created successfully.
What I'm notice is that the on data or on error is never fired? Im new using level so any help will be appreciate.

This solve the issue;
I just realize that I need to wait for the Stream close:
class LevelDB {
constructor() {
this.db = level(chainDB);
}
getAllItems() {
let self = this;
let dataArray = [];
return new Promise(function(resolve, reject){
self.db.createKeyStream()
.on('data', function (data) {
dataArray.push(data);
})
.on('error', function (err) {
reject(err)
})
.on('close', function () {
resolve(dataArray);
})
}
}

Related

Leaflet mapping: Assign object to fetch promise for local GeoJSON file

I am looking to assign as an object a Fetch API promise from a local GeoJSON file.
Here is the code
fetch("data/sites.geojson")
.then(function(response) {
return response.json();
})
.then(function(data) {
L.geoJSON(data, {
pointToLayer: styles_sites
}).addTo(map);
});
};
I tried the call back method, as advised here
Saving fetched JSON into variable
(EDIT) New code, but there is still a missing formal parameter
function getData("data/sites.geojson", cb) {
fetch("data/sites.geojson")
.then(function(response) {
return response.json();
})
.then(function(data) {
L.geoJSON(data, {
pointToLayer: styles_sites,
onEachFeature: function (feature, layer) {
layer.on('mouseover', function() {
layer.openPopup(layer.bindPopup("<b>"+feature.properties.nombre+"</b>"))
});
layer.on('mouseout', function() {
layer.closePopup();
});
layer.on('click', function () {
layer.bindPopup("<b>Nombre: </b>"+feature.properties.nombre+"<br><b>Barrio: </b>"+feature.properties.barrio+"<br><b>Tipo: </b>"+feature.properties.tipo+"<br><b>Ubicacion: </b>"+feature.properties.ubicacion+"<br><b>Correo: </b>"+feature.properties.contacto);
});
}
}).addTo(map);
.then(function(result) {
cb(result);
});
});
};
getData("data/sites.geojson", function (data) {
return console.log({data});
});
Most probably just incorrect syntax of your callback function:
// Use either arrow function
getData("data/sites.geojson", (data) => {
return console.log({data});
});
// or standard function
getData("data/sites.geojson", function (data) {
return console.log({data});
});
I found the way to work this out by adding within the fetch function, what I originally wanted to do on the map.
This was to add a L.controlLayer using the geojson as overlay.
This is the code that made it work:
let sites = getData()
.then((function(data) {
L.geoJSON(data, {
pointToLayer: styles_sites,
onEachFeature: function LayerControl(feature, layer) {
var popupText = "<b>" + feature.properties.nombre + "<br>";
layer.bindPopup(popupText);
category = feature.properties.tipo;
// Initialize the category array if not already set.
if (typeof categories[category] === "undefined") {
categories[category] = L.layerGroup().addTo(map);
layersControl.addOverlay(categories[category], category);
}
categories[category].addLayer(layer);
layer.on('mouseover', function() {
layer.openPopup(layer.bindPopup("<b>"+feature.properties.nombre+"</b>"))
});
layer.on('mouseout', function() {
layer.closePopup();
});
layer.on('click', function () {
layer.bindPopup("<b>Nombre: </b>"+feature.properties.nombre+"<br><b>Barrio: </b>"+feature.properties.barrio+"<br><b>Tipo: </b>"+feature.properties.tipo+"<br><b>Ubicacion: </b>"+feature.properties.ubicacion+"<br><b>Correo: </b>"+feature.properties.contacto);
});
}
}).addTo(map);
}));
Actually it comes from one of your answer on another post ghybs.

Nodejs - xmlhttprequest promise never finish

module.js
var Promise = require('bluebird');
var XMLHttpRequest = require('xhr2');
function fetchdata(id) {
var url = 'http://www.youtube.com/watch?v=' + id;
return new Promise(function (fulfill, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
var jsonStr;
try {
fulfill(xhr.response);
} catch (e) {
reject(jsonStr);
}
};
xhr.onerror = function(e) {
reject(e);
};
xhr.send('');
});
}
module.exports = {
getdata: function (videoID) {
return new Promise(function (fulfill, reject) {
if (!videoID) {
reject(new Error('Unable to get video id.'));
return;
}
fetchdata(videoID).then(
function (d) {
console.log( d);
}
);
});
}
};
index.js
var parser = require('./module.js');
parser.getdata("ZI4tRn4dOGg", function (data) {
console.log(data);
}
)
I tried to get youtube view page source code with xmlhttprequest.
BUT above code does not finish. I guess it is waiting for something.
Is problem from bluebird? or xhr2? and why does this code never finish?
Your xhr instance had a memory leak, might be a problem with the library, last publish was a year ago. Bluebird was ok. You can fix the hangup by using node-fetch and dropping in this replacement for fetchdata
const fetch = require('node-fetch')
function fetchdata(id) {
var url = 'http://www.youtube.com/watch?v=' + id;
return fetch(url).then(res => res.text())
}

Unit Test Breeze calls for Aurelia

I have converted my fetch calls to use breeze.EntityQuery but how can I write my unit tests to mock the breeze client? Here is my code for the unit test fetch call that I'm trying to write for breeze.
class HttpStub {
fetch(url) {
var response = this.itemStub;
this.url = url;
return new Promise((resolve) => {
resolve({ json: () => response });
});
}
configure(func) {}
}
describe('Order', () => {
var sut, http, itemStubs, itemFake;
beforeEach(() => {
http = new HttpStub();
sut = new Order(http);
itemStubs = [1];
itemFake = [2];
http.itemStub = itemStubs;
});
describe('getOrders', () => {
it('should return orders', (done) => {
var info = new Info("1", "C", null, null);
sut.getOrders(info).then(result => {
expect(result).toBe(itemStubs);
expect(result).not.toBe(itemFake);
done();
});
});
});
});
Try using the jasmine spyOn function with callFake. Jasmine's spies are an easier way to mock a function call.
beforeEach(function () {
spyOn(httpClient, "fetch").and.callFake(function () {
return new Promise(function (resolve, reject) {
var fetchResponse = "the fake response";
resolve(fetchResponse);
});
});
});
An example (with TypeScript)
import { HttpClient } from "aurelia-fetch-client";
import { autoinject, Container } from "aurelia-framework";
#autoinject
export class DemoClass {
constructor(private httpClient: HttpClient) { }
public UseTheHttpClient() {
return this.httpClient.fetch("some_url");
}
}
describe("the demo class", function () {
let container: Container = new Container();
let httpClient: HttpClient = new HttpClient(); // create an http client
container.registerInstance(HttpClient, httpClient);
let demoClass: DemoClass = container.get(DemoClass);
beforeEach(function () { // spy on that HTTP client
spyOn(httpClient, "fetch").and.callFake(function () {
return new Promise(function (resolve, reject) {
resolve("some_fake_response");
});
});
});
it("returns the fake response", function (done) {
demoClass.UseTheHttpClient().then((response) => {
expect(response).toBe("some_fake_response");
done();
});
});
});

Promise chaining with Q nodejs

I have this code here but I cannot figure out why the second FS.readfile() executes last. Basically I want to:
readFile(passwd)
.then(console(textpasswd)
.then(readFile(hosts).
.then(console(texthosts);
But what happens is the readFile(hosts) executes last in the chain. I sense something is wrong with my deferring, but what?
Here is the code
module.paths.push('/usr/local/lib/node_modules');
var Q = require('q');
var FS=require('fs');
var deferred = Q.defer();
var p=deferred.promise;
FS.readFile("/etc/passwd", "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
});
p.then(function (text) {
console.log(text);
}).then(FS.readFile("/etc/hosts", "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
})
).then(function (text) {
console.log(text);
});
The deferred must obtain a new promise to continue the chain correctly. Also notice the first (function())() must be executed to return a promise although you don't need to do it this way. So here is my solution. (I cleaned it up a little):
module.paths.push('/usr/local/lib/node_modules');
var Q = require('q');
var FS = require('fs');
(function () {
var deferred = Q.defer();
FS.readFile("/etc/passwd", "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
})
return deferred.promise;
})().then(function (text) {
console.log(text);
}).then(function () {
var deferred = Q.defer();
FS.readFile("/etc/hosts", "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
})
return deferred.promise;
}).then(function (text) {
console.log(text);
}).then(function () {
console.log('The End');
});

AngularJS - Strange behaviour of promises in connection with notify()

As I want to implement a chat in AngularJS, I want to use the promise/deferred principle. My ChatService looks like the following:
factory('ChatService', ['$q', '$resource', function($q, $resource) {
var Service = {};
var connected = false;
var connection;
var chatResource = $resource('/guitars/chat/:action', {action: '#action'}, {
requestChatroomId: {
params: {
action: 'requestChatroomId'
},
method: 'GET'
},
sendMessage: {
params: {
action: 'sendMessage'
},
method: 'POST'
}
});
Service.connect = function(cb) {
var deferred = $q.defer();
chatResource.requestChatroomId(function(data) {
connection = new WebSocket('ws://127.0.0.1:8888/realtime/' + data.chatroomId);
connection.onerror = function (error) {
deferred.reject('Error: ' + error);
};
connection.onmessage = function (e) {
cb.call(this, e.data);
deferred.notify(e.data);
};
connected = true;
});
return deferred.promise;
};
Service.sendMessage = function(msg) {
if(!connected) {
return;
}
chatResource.sendMessage({message: msg});
}
return Service;
}])
My controller using the ChatService is:
app.controller('ChatCtrl', ['$scope', 'ChatService', function($scope, ChatService) {
$scope.chat = {};
$scope.chat.conversation = [];
var $messages = ChatService.connect(function(message) {
$scope.$apply(function() {
// #1 THIS FIRES EVERY TIME
$scope.chat.conversation.push(message);
});
});
$messages.then(function(message) {
console.log('Finishes - should never occur!')
}, function(error) {
console.log('An error occurred!')
}, function(message) {
// #2 THIS FIRES ONLY IF THERE IS AN INTERACTION WITH THE ANGULAR MODEL
console.log(message);
});
$scope.sendMessage = function(event) {
ChatService.sendMessage($scope.chat.message);
$scope.chat.message = '';
};
}]);
If something is pushed from the server, callback #1 is called, but callback #2 wont be called until there is some interaction with the angular-model, i.e. start writing something in the input-Box. What is the reason for that behaviour?
Okay the reason was, that AngularJS was not aware of a change. So I injected the $rootScope to my ChatService:
factory('ChatService', ['$q', '$resource', '$rootScope', function($q, $resource, $rootScope) {
and in connection.onmessage I called $apply() on $rootScope:
connection.onmessage = function (e) {
deferred.notify(e.data);
$rootScope.$apply();
};

Resources