\n
Here is the error that I'm getting.\n
Here is the code of my script.js
\n(function() {\n\n 'use strict';\n\n var $ = document.querySelector.bind(document);\n\n var ENTER_KEY = 13;\n var newTodoDom = document.getElementById('new_todo');\n var syncDom = document.getElementById('sync-wrapper');\n \n\n // EDITING STARTS HERE (you dont need to edit anything above this line)\n var NodePouchDB = require('pouchdb');\n var db = new NodePouchDB('todos');\n\n var couchdb = require('felix-couchdb')\n var remoteCouch = couchdb.createClient(5984, 'https://ac725f4e-29ec-4614-8e96-02ebc74a529b-bluemix.cloudant.com/')\n\n\n db.info(function(err, info) {\n console.log(\"is working\", info)\n db.changes({\n since: info.update_seq,\n live: true\n }).on('change', showTodos);\n });\n\n // We have to create a new todo document and enter it in the database\n function addTodo(text) {\n var todo = {\n _id: new Date().toISOString(),\n title: text,\n completed: false\n };\n db.put(todo).then(function (result) {\n console.log(\"everything is A-OK\");\n console.log(result);\n }).catch(function (err) {\n console.log('everything is terrible');\n console.log(err);\n });\n }\n\n // Show the current list of todos by reading them from the database\n function showTodos() {\n db.allDocs({include_docs: true, descending: true}).then(function(doc) {\n redrawTodosUI(doc.rows);\n }).catch(function (err) {\n console.log(err);\n });\n }\n\n function checkboxChanged(todo, event) {\n todo.completed = event.target.checked;\n console.log(todo);\n db.put(todo);\n }\n\n // User pressed the delete button for a todo, delete it\n function deleteButtonPressed(todo) {\n db.remove(todo);\n }\n\n // The input box when editing a todo has blurred, we should save\n // the new title or delete the todo if the title is empty\n function todoBlurred(todo, event) {\n var trimmedText = event.target.value.trim();\n if (!trimmedText) {\n db.remove(todo);\n } else {\n todo.title = trimmedText;\n db.put(todo);\n }\n }\n\n // Initialise a sync with the remote server\n function sync() {\n syncDom.setAttribute('data-sync-state', 'syncing');\n \n var opts = {live: true};\n db.sync(remoteCouch, opts, syncError);\n }\n\n // EDITING STARTS HERE (you dont need to edit anything below this line)\n\n // There was some form or error syncing\n function syncError() {\n syncDom.setAttribute('data-sync-state', 'error');\n }\n\n // User has double clicked a todo, display an input so they can edit the title\n function todoDblClicked(todo) {\n var div = document.getElementById('li_' + todo._id);\n var inputEditTodo = document.getElementById('input_' + todo._id);\n div.className = 'editing';\n inputEditTodo.focus();\n }\n\n // If they press enter while editing an entry, blur it to trigger save\n // (or delete)\n function todoKeyPressed(todo, event) {\n if (event.keyCode === ENTER_KEY) {\n var inputEditTodo = document.getElementById('input_' + todo._id);\n inputEditTodo.blur();\n }\n }\n\n // Given an object representing a todo, this will create a list item\n // to display it.\n function createTodoListItem(todo) {\n var checkbox = document.createElement('input');\n checkbox.className = 'toggle';\n checkbox.type = 'checkbox';\n checkbox.addEventListener('change', checkboxChanged.bind(this, todo));\n\n var label = document.createElement('label');\n label.appendChild( document.createTextNode(todo.title));\n label.addEventListener('dblclick', todoDblClicked.bind(this, todo));\n\n var deleteLink = document.createElement('button');\n deleteLink.className = 'destroy';\n deleteLink.addEventListener( 'click', deleteButtonPressed.bind(this, todo));\n\n var divDisplay = document.createElement('div');\n divDisplay.className = 'view';\n divDisplay.appendChild(checkbox);\n divDisplay.appendChild(label);\n divDisplay.appendChild(deleteLink);\n\n var inputEditTodo = document.createElement('input');\n inputEditTodo.id = 'input_' + todo._id;\n inputEditTodo.className = 'edit';\n inputEditTodo.value = todo.title;\n inputEditTodo.addEventListener('keypress', todoKeyPressed.bind(this, todo));\n inputEditTodo.addEventListener('blur', todoBlurred.bind(this, todo));\n\n var li = document.createElement('li');\n li.id = 'li_' + todo._id;\n li.appendChild(divDisplay);\n li.appendChild(inputEditTodo);\n\n if (todo.completed) {\n li.className += 'complete';\n checkbox.checked = true;\n }\n\n return li;\n }\n\n function redrawTodosUI(todos) {\n var ul = document.getElementById('todo-list');\n ul.innerHTML = '';\n todos.forEach(function(todo) {\n ul.appendChild(createTodoListItem(todo.doc));\n });\n }\n\n function newTodoKeyPressHandler( event ) {\n if (event.keyCode === ENTER_KEY) {\n addTodo(newTodoDom.value);\n newTodoDom.value = '';\n }\n }\n\n function addEventListeners() {\n newTodoDom.addEventListener('keypress', newTodoKeyPressHandler, false);\n }\n\n addEventListeners();\n showTodos();\n\n if (remoteCouch) {\n sync();\n }\n\n})();
\r\nReputation: 107
I'm creating an electron app using pouchDB and I want the app to be able to diferents customers and sync the data between them. As an example I'm making the tutorial: https://github.com/nolanlawson/pouchdb-getting-started-todo, I adapt the code to electron and I created a noSQL database at cloudant.
At the moment I can save data but I cannot sync with my remote db that is in cloudant. Here is the endpoint I'm using to sync data between both database.
Here is the error that I'm getting.
Here is the code of my script.js
(function() {
'use strict';
var $ = document.querySelector.bind(document);
var ENTER_KEY = 13;
var newTodoDom = document.getElementById('new_todo');
var syncDom = document.getElementById('sync-wrapper');
// EDITING STARTS HERE (you dont need to edit anything above this line)
var NodePouchDB = require('pouchdb');
var db = new NodePouchDB('todos');
var couchdb = require('felix-couchdb')
var remoteCouch = couchdb.createClient(5984, 'https://ac725f4e-29ec-4614-8e96-02ebc74a529b-bluemix.cloudant.com/')
db.info(function(err, info) {
console.log("is working", info)
db.changes({
since: info.update_seq,
live: true
}).on('change', showTodos);
});
// We have to create a new todo document and enter it in the database
function addTodo(text) {
var todo = {
_id: new Date().toISOString(),
title: text,
completed: false
};
db.put(todo).then(function (result) {
console.log("everything is A-OK");
console.log(result);
}).catch(function (err) {
console.log('everything is terrible');
console.log(err);
});
}
// Show the current list of todos by reading them from the database
function showTodos() {
db.allDocs({include_docs: true, descending: true}).then(function(doc) {
redrawTodosUI(doc.rows);
}).catch(function (err) {
console.log(err);
});
}
function checkboxChanged(todo, event) {
todo.completed = event.target.checked;
console.log(todo);
db.put(todo);
}
// User pressed the delete button for a todo, delete it
function deleteButtonPressed(todo) {
db.remove(todo);
}
// The input box when editing a todo has blurred, we should save
// the new title or delete the todo if the title is empty
function todoBlurred(todo, event) {
var trimmedText = event.target.value.trim();
if (!trimmedText) {
db.remove(todo);
} else {
todo.title = trimmedText;
db.put(todo);
}
}
// Initialise a sync with the remote server
function sync() {
syncDom.setAttribute('data-sync-state', 'syncing');
var opts = {live: true};
db.sync(remoteCouch, opts, syncError);
}
// EDITING STARTS HERE (you dont need to edit anything below this line)
// There was some form or error syncing
function syncError() {
syncDom.setAttribute('data-sync-state', 'error');
}
// User has double clicked a todo, display an input so they can edit the title
function todoDblClicked(todo) {
var div = document.getElementById('li_' + todo._id);
var inputEditTodo = document.getElementById('input_' + todo._id);
div.className = 'editing';
inputEditTodo.focus();
}
// If they press enter while editing an entry, blur it to trigger save
// (or delete)
function todoKeyPressed(todo, event) {
if (event.keyCode === ENTER_KEY) {
var inputEditTodo = document.getElementById('input_' + todo._id);
inputEditTodo.blur();
}
}
// Given an object representing a todo, this will create a list item
// to display it.
function createTodoListItem(todo) {
var checkbox = document.createElement('input');
checkbox.className = 'toggle';
checkbox.type = 'checkbox';
checkbox.addEventListener('change', checkboxChanged.bind(this, todo));
var label = document.createElement('label');
label.appendChild( document.createTextNode(todo.title));
label.addEventListener('dblclick', todoDblClicked.bind(this, todo));
var deleteLink = document.createElement('button');
deleteLink.className = 'destroy';
deleteLink.addEventListener( 'click', deleteButtonPressed.bind(this, todo));
var divDisplay = document.createElement('div');
divDisplay.className = 'view';
divDisplay.appendChild(checkbox);
divDisplay.appendChild(label);
divDisplay.appendChild(deleteLink);
var inputEditTodo = document.createElement('input');
inputEditTodo.id = 'input_' + todo._id;
inputEditTodo.className = 'edit';
inputEditTodo.value = todo.title;
inputEditTodo.addEventListener('keypress', todoKeyPressed.bind(this, todo));
inputEditTodo.addEventListener('blur', todoBlurred.bind(this, todo));
var li = document.createElement('li');
li.id = 'li_' + todo._id;
li.appendChild(divDisplay);
li.appendChild(inputEditTodo);
if (todo.completed) {
li.className += 'complete';
checkbox.checked = true;
}
return li;
}
function redrawTodosUI(todos) {
var ul = document.getElementById('todo-list');
ul.innerHTML = '';
todos.forEach(function(todo) {
ul.appendChild(createTodoListItem(todo.doc));
});
}
function newTodoKeyPressHandler( event ) {
if (event.keyCode === ENTER_KEY) {
addTodo(newTodoDom.value);
newTodoDom.value = '';
}
}
function addEventListeners() {
newTodoDom.addEventListener('keypress', newTodoKeyPressHandler, false);
}
addEventListeners();
showTodos();
if (remoteCouch) {
sync();
}
})();
Upvotes: 1
Views: 667
Reputation: 3737
To get to where the problem sits, can you verify that you can speak to the Cloudant database normally, that is using curl
from the command-line? Using curl
, fetch a document by its _id, perhaps a document you created manually using the Cloudant dashboard. That should shake out any problems with authentication: I note you're using IAM, which isn't always straight-forward -- and to my knowledge, not supported by PouchDB (or wasn't, last time I looked).
If that is the problem, create a new Cloudant instance with IAM+Legacy credentials.
Upvotes: 3