CouchDB save doc uri incorrect - post

I am really scratching my head over this one. I am using cloudant as my couchdb provider and attempting to save a document to my db. I first establish what my db is in the following code.
db = jQuery.couch.db("https://fullscore.cloudant.com/fullscore");
I do a console.log and see that the URI and name are correct in the object.
db.saveDoc(doc, {
success: function(data) {
console.log("posted");
},
error: function(status) {
console.log("failure");
}
});
However when I look at the post, it posts to: https://cloudant.com/https%3A%2F%2Ffullscore.cloudant.com%2Ffullscore/
Which as you can see is totally wrong. Obviously there is something built in to append onto cloudant.com ... anyone know how this can be overridden? Or perhaps its some other problem? I'm getting a 404 error.
--Ashley

To clarify the exact code, you need to set the URL prefix on your couch object, then use just the db name:
$.couch.urlPrefix = "https://username.cloudant.com";
var db = $.couch.db("dbname");

I'm guessing you're passing the full database URL in a place where you're only supposed to pass the database name (i.e. the latter "fullscore" part in https://fullscore.cloudant.com/fullscore"). You've probably already set up the host name at some earlier point in your program.

Related

Cypress unable to save current URL with `as`

My web app generates a UUIDv4 for every new 'post', and each post has its own URL like /posts/<uuid>. I'm not able to predict what uuid gets generated, and therefore I'm unable to go back to a specific post that was created earlier during testing.
According to the docs, cy.url() returns the URL as a string. I tried saving the URL using a .as(), but it didn't work:
cy.url().as('postUrl');
// go somewhere else
cy.visit('#postUrl');
// ends up visiting `localhost:3000/#postUrl`
I saw in another SO question that I should use .then on cy.url(), but that didn't work either:
cy.url().then(url => url).as('postUrl');
// go somewhere else
cy.visit('#postUrl');
How do I save the current URL for use later?
Found the answer buried in later pages of a google search. In order to use the saved URL, use cy.get('#postUrl') and call a .then with a callback that visits that url.
cy.url().as('postUrl');
// go somewhere else
cy.get('#postUrl').then(url => {
cy.visit(url);
}
var currentUrl=''
cy.url().then(url => {
currentUrl = url;
});
cy.visit(currentUrl)

get 'undefined' saveResult.entities after saveChanges [breezejs]

I tried to update one entity in my angularjs client using breezejs library. After calling saveChanges(), it can actually save back in the server and fetched on the client. However, the server did not return the response back. The saveResult.entities is undefined and pop up an error for me. When I took a look at the docs, it mentions 'Some service APIs do not return information about every saved entity. If your server doesn't return such information, you should add the pre-save, cached entity to saveResult.entities yourself'. Could anyone provide an example of how to do this?
This is the code when i am trying to do an update.
manager.saveChanges(entitiesToSave, null, (saveResult) => {
const savedRes = saveResult;
savedRes.entities = entitiesToSave;
return savedRes;
}).then(saveSucceeded);
On the server, you would need to construct the response for an update similar to the way it is for a create:
response.setContent(...); // entities
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());

Mongo and Node.js: unable to look up document by _id

I'm using the Express framework and Mongodb for my application.
When I insert objects into the database, I use a custom ObjectID. It's generated using mongo's objectid function, but toString()ed (for reasons i think are irrelevant, so i won't go into them). Inserts look like this:
collection.insert({ _id: oid, ... })
The data is in mongo - i can db.collection.find().pretty() from mongo's cli and see it exactly as it's supposed to be. But when I run db.collection.find({_id: oid}) from my application, I get nothing ([] to be exact). Even when I manually hardcode the objectID that I'm looking for (that I know is in the database) into my query, I still can't get an actual result.
As an experiment, I tried collection.find({title: "test title"}) from the app, and got exactly the result I wanted. The database connection is fine, the data structure is fine, the data itself is fine - the problem is clearly with the _id field.
Query code:
var sid = req.param("sid");
var db = req.db;
var collection = db.get("stories");
collection.find({'_id': sid }, function(err, document) {
console.log(document.title);
});
Any ideas on how I can get my document by searching _id?
update: per JohnnyHK's answer, I am now using findOne() as in the example below. However, it now returns null (instead of the [] that i was getting). I'll update if I find a solution on my own.
collection.findOne({'_id': sid }, function(err, document) {
console.log(document);
});
find provides a cursor as the second parameter to its callback (document in your example). To find a single doc, use findOne instead:
collection.findOne({'_id': sid }, function(err, document) {
console.log(document.title);
});

Breeze get Metadata from another location

I think I have a small stupid problem with breeze.
Maybe somebody know the answer? I wanna change the url where breeze is looking for the metadata.
breeze tries to connect to:
http://localhost:7048/DynamicsNAV71/OData/Company('CRONUS%20(Schweiz)%20AG')/Metadata
instead of where it is:
http://localhost:7048/DynamicsNAV71/OData/$metadata
Somebody knows how to change this?
Regards
If you only want to change the metadata location, one approach would be to intercept the ajax call and change the url like this:
var ajaxAdapter = breeze.config.getAdapterInstance('ajax');
ajaxAdapter.requestInterceptor = function (requestInfo) {
if (requestInfo.config.url === "http://localhost:7048/DynamicsNAV71/OData/Company('CRONUS%20(Schweiz)%20AG')/Metadata") {
requestInfo.config.url = "http://localhost:7048/DynamicsNAV71/OData/$metadata";
}
}
For more info see the Configure a specific AJAX request with requestInterceptor topic here:
http://www.breezejs.com/documentation/controlling-ajax

Modify URL before loading page in firefox

I want to prefix URLs which match my patterns. When I open a new tab in Firefox and enter a matching URL the page should not be loaded normally, the URL should first be modified and then loading the page should start.
Is it possible to modify an URL through a Mozilla Firefox Addon before the page starts loading?
Browsing the HTTPS Everywhere add-on suggests the following steps:
Register an observer for the "http-on-modify-request" observer topic with nsIObserverService
Proceed if the subject of your observer notification is an instance of nsIHttpChannel and subject.URI.spec (the URL) matches your criteria
Create a new nsIStandardURL
Create a new nsIHttpChannel
Replace the old channel with the new. The code for doing this in HTTPS Everywhere is quite dense and probably much more than you need. I'd suggest starting with chrome/content/IOUtils.js.
Note that you should register a single "http-on-modify-request" observer for your entire application, which means you should put it in an XPCOM component (see HTTPS Everywhere for an example).
The following articles do not solve your problem directly, but they do contain a lot of sample code that you might find helpful:
https://developer.mozilla.org/en/Setting_HTTP_request_headers
https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads
Thanks to Iwburk, I have been able to do this.
We can do this my overriding the nsiHttpChannel with a new one, doing this is slightly complicated but luckily the add-on https-everywhere implements this to force a https connection.
https-everywhere's source code is available here
Most of the code needed for this is in the files
IO Util.js
ChannelReplacement.js
We can work with the above files alone provided we have the basic variables like Cc,Ci set up and the function xpcom_generateQI defined.
var httpRequestObserver =
{
observe: function(subject, topic, data) {
if (topic == "http-on-modify-request") {
var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
var requestURL = subject.URI.spec;
if(isToBeReplaced(requestURL)) {
var newURL = getURL(requestURL);
ChannelReplacement.runWhenPending(subject, function() {
var cr = new ChannelReplacement(subject, ch);
cr.replace(true,null);
cr.open();
});
}
}
},
get observerService() {
return Components.classes["#mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
},
register: function() {
this.observerService.addObserver(this, "http-on-modify-request", false);
},
unregister: function() {
this.observerService.removeObserver(this, "http-on-modify-request");
}
};
httpRequestObserver.register();
The code will replace the request not redirect.
While I have tested the above code well enough, I am not sure about its implementation. As far I can make out, it copies all the attributes of the requested channel and sets them to the channel to be overridden. After which somehow the output requested by original request is supplied using the new channel.
P.S. I had seen a SO post in which this approach was suggested.
You could listen for the page load event or maybe the DOMContentLoaded event instead. Or you can make an nsIURIContentListener but that's probably more complicated.
Is it possible to modify an URL through a Mozilla Firefox Addon before the page starts loading?
YES it is possible.
Use page-mod of the Addon-SDK by setting contentScriptWhen: "start"
Then after completely preventing the document from getting parsed you can either
fetch a different document from the same domain and inject it in the page.
after some document.URL processing do a location.replace() call
Here is an example of doing 1. https://stackoverflow.com/a/36097573/6085033

Resources