I am pretty new to pouchDB and couchDB. I've trying to use pouchdb find but having some problems.
I have created a view "test" and source -
function(doc) {
emit(doc.name, doc.occupation);
}
and when i run this -
localDB.query('test/test').then(function (res) {
console.log(res);
}).catch(function (err) {
console.log(err);
});
Everything works as expected.
But when i try pouchdb find -
localDB.find({
selector: {name: 'kittens'}
}).then(function (result) {
console.log(result);
}).catch(function (err) {
console.log(err);
});
I got following error -
Error: couldn't find a usable index. try creating an index on: name.
If i create index by
localDB.createIndex({
index: {
fields: ['name']
}
});
only then pouchdb find code works. But when i manually created an index (shown in image above) then it doesn't.
Any help is appreciated. Thanks in advance.
pouchdb-find uses the new "Mango" query language, which is different from map/reduce. Mango is only supported in CouchDB 2.0+ and PouchDB Server, not CouchDB 1.x.
So at this time you will need to either use CouchDB 2.0 or PouchDB Server with pouchdb-find if you want it to work on both the client and the server, or you will need to use regular map/reduce instead and avoid pouchdb-find.
Related
I have a local PouchDB that must sync with a remote CouchDB. I'm trying (so far without success) to replicate the local one (on localhost:8081) to the remote one (on localhost:5984) through a POST call to /_replicate.
No matter what I enter in the "source" field of my request, it appends "http:127.0.0.1:5984/" to the source url (or name) and, obviously, I get an error saying it can't find the source db. Of course.
My question (which is, by the way, my very first question on StackOverFlow so please be indulgent) is then: how can I point to the correct db?
Thanks!
let url = `http://127.0.0.1:5984/_replicate`;
let data = {
// This is where I struggle
"source" : "http://127.0.0.1:8081/_pouch_local_db",
"target" : `http://127.0.0.1:5984/mydb-${username}`
}
fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
}).then(response => {
console.log('Success syncing: ', response);
}).catch(error => console.error('Error while syncing: ', error));
Your code suggests you are asking CouchDb to replicate with a local PouchDb database? That will not work as PouchDb is intended to be a client-side database system. In normal use, PouchDb is not a server (I know there is a PouchDb server available but I do not think that is of help here).
You should use the PouchDb API (include the PouchDB library in your Javascript code) and tell your local PouchDb database to replicate with the CouchDb server on port 5984.
I would expect your code to look like this:
var dblocal = new PouchDB('local_db');
var dbremote = new PouchDB('http://127.0.0.1:5984/myremotedb');
dblocal.replicate.to(dbremote, function (err, result) {
if (err) { return console.log(err); }
// handle 'completed' result
});
I am assuming you have not password-protected your test CouchDb database. If you have, you will need to include a username and password in the "auth" options for the remote database when using new PouchDB.
There are some examples and information on the PouchDB site.
I am using the mean.js stack. I need to upload a Microsoft excel doc (.xls) and parse through the items. With that information I would like to create a new object from a schema to upload to a mongo database. I'm not sure where to even begin.
I have the schema for mongoose made. I really need help with the parsing and then saving to mongo. If there are any guides or suggested node packages, they would be great appreciated. Thank you.
first you have to convert your data to json. you can use a npm library like excel-to-json. then once you convert your data you can bulk insert them to mongodb with mongoose via your model.
excel2json(configuration, function(err, result) {
if(err) {
console.error(err);
} else {
Model.create(result, function(err, docs) {
if(!err){
console.log('inserted');
}
});
}
});
This is my first post here so please don't get mad if my formatting is a bit off ;-)
I'm trying to develop a backend solution using Azure mobile apps and node.js for server side scripts. It is a steep curve as I am new to javaScript and node.js coming from the embedded world. What I have made is a custom API that can add users to a MSSQL table, which is working fine using the tables object. However, I also need to be able to delete users from the same table. My code for adding a user is:
var userTable = req.azureMobile.tables('MyfUserInfo');
item.id = uuid.v4();
userTable.insert(item).then( function (){
console.log("inserted data");
res.status(200).send(item);
});
It works. The Azure node.js documentation is really not in good shape and I keep searching for good example on how to do simple things. Pretty annoying and time consuming.
The SDK documentation on delete operations says it works the same way as read, but that is not true. Or I am dumb as a wet door. My code for deleting looks like this - it results in exception
query = queries.create('MyfUserInfo')
.where({ id: results[i].id });
userTable.delete(query).then( function(delet){
console.log("deleted id ", delet);
});
I have also tried this and no success either
userTable.where({ id: item.id }).read()
.then( function(results) {
if (results.length > 0)
{
for (var i = 0; i < results.length; i++)
{
userTable.delete(results[i].id);
});
}
}
Can somebody please point me in the right direction on the correct syntax for this and explain why it has to be so difficult doing basic stuff here ;-) It seems like there are many ways of doing the exact same thing, which really confuses me.
Thanks alot
Martin
You could issue SQL in your api
var api = {
get: (request, response, next) => {
var query = {
sql: 'UPDATE TodoItem SET complete=#completed',
parameters: [
{ name: 'completed', value: request.params.completed }
]
};
request.azureMobile.data.execute(query)
.then(function (results) {
response.json(results);
});
}};
module.exports = api;
That is from their sample on GitHub
Here is the full list of samples to take a look at
Why are you doing a custom API for a table? Just define the table within the tables directory and add any custom authorization / authentication.
Nano doesn't provide documentation for temporary views, is there any undocumented method? Failing that, how would you advise somebody execute a temporary view using a nano-like syntax. Currently I am attempting to create the view as _view/guid, query it, return the results, and then delete it from the collection:
function generateToken() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
var db = nano.use('db'),
fn = 'function(doc){ emit(doc); }',
token = generateToken(),
id = '_design/' + token;
db.insert({ views: { view: { map: fn } } }, id, function(){
db.view(token, 'view', function (err, results) {
db.get(id, function (err, view) {
console.log(results);
db.destroy(id, view._rev);
});
});
});
I assume this is inoptimal with the temporary view functionality built into couch core.
I'm aware of the temporary-view caveats, however I do believe I have a genuine usage case.
The temporary view API is described in the official CouchDB documentation: http://docs.couchdb.org/en/latest/api/database/temp-views.html#post--db-_temp_view
Open up futon and see what calls it does to the couchDB api ?
Edit: went and did the above
Futon does a post to SVRNAME/DBNAME/_temp_view?limit=11&descending=true
request payload {language: "javascript" map: function(doc) { emit(null, doc.id);}
and you must be logged in as an admin.
Hope that helps
I think you could do this through Nano using nano.request() (or nano.dinosaur()). https://github.com/dscape/nano#nanorequestopts-callback
I am trying nTwitter (nodejs v0.6.10) - I tried using the example code for searching twitter but I get the following error when trying the search function for the library (the keys I am using appear to be correct):
Cannot set property 'q' of null
Any ideas what might be causing this issue - stack trace is copied below (so is the code)?
//twit is instance of Twitter (with keys assigned in)
twit.search('nodejs', function(err, data) {
if (err) {
console.log('Twitter search failed!');
}
else {
console.log('Search results:');
console.dir(data);
}
});
at Object.merge (/opt/testDir/node/node_modules/ntwitter/lib/utils.js:9:18)
at Twitter.search (/opt/testDir/node/node_modules/ntwitter/lib/twitter.js:167:18)
at Object.search (/opt/testDir/node/projects/testApp/public/javascripts/nTwitterTest.js:13:6)
at nTwitterTestMediator (/opt/testDir/node/projects/testApp/app.js:1188:14)
at callbacks (/opt/testDir/node/projects/testApp/node_modules/express/lib/router/index.js:272:11)
at param (/opt/testDir/node/projects/testApp/node_modules/express/lib/router/index.js:246:11)
at pass (/opt/testDir/node/projects/testApp/node_modules/express/lib/router/index.js:253:5)
at Router._dispatch (/opt/testDir/node/projects/testApp/node_modules/express/lib/router/index.js:280:4)
at Object.handle (/opt/testDir/node/projects/testApp/node_modules/express/lib/router/index.js:45:10)
at next (/opt/testDir/node/projects/testApp/node_modules/connect/lib/http.js:201:15)
Pilot error - the settings on twitter were not updated (from read only to read-write-execute).
I switched to read & write (instead of read, write and execute) and the settings changed (this was due to my ignorance of twitter api's & permission levels).
I was able to post tweets as well as access the streaming API (with the code on the github page) with the read-write access level for the twitter app.
I am still unable to use the search feature (code below).
twit.search('nodejs', function(err, data) {
if (err) {
console.log('Twitter search failed!');
}
else {
console.log('Search results:');
console.dir(data);
}
});
Thanks
I had this problem as well. You are missing one parameter.
Try this:
twit.search('nodejs', {}, function(err, data){
I think this was an error in the original ntwitter documentation.
Pilot error - the settings on twitter were not updated (from read only to read-write-execute).
I switched to read & write (instead of read, write and execute) and the settings changed (this was due to my ignorance of twitter api's & permission levels).
I was able to post tweets as well as access the streaming API (with the code on the github page) with the read-write access level for the twitter app.
I am still unable to use the search feature (code below).
twit.search('nodejs', function(err, data) {
if (err) {
console.log('Twitter search failed!');
}
else {
console.log('Search results:');
console.dir(data);
}
});
Thanks.