I am working on an application that uses MongoDB for data persistence, alongside the mongoose library to allow angularjs to communicate with it. I'm using Postman to test my routes, and that's where the problem comes in.
In MongoDB, I inserted an entry thusly:
db.posts.insert{{id:6, author:"Blah", area:"Sport", body:"bleh", user_id:6, comments:[{author:"foo", body:"bar"}]})
I then queried it through Postman, and it successfully returned the Post, but there was a problem. The comments array had been returned as a strange data type, object Object, and I could not access it from my HTML files.
This is what Postman retrieved:
{"_id":"5536112cffc7bf00b2585d24","id":6,"author":"Blah","area":"Sport","body":"bleh","user_id":6,"comments":["[object Object]"]}]
I want to be able to access the author and body elements of comments independently, or else to be able to get at them from my http files. Does anyone know how I might stop this Object-ification from happening, or failing that, how I might query the data from this new Object?
Thanks.
Instead of passing complete db object we can pass required parameters. below is the example to form the required parameters.
res.json({"author":item.author,"body":item.body});
Here You can find call back function and array conversion from the cursor
Related
I am able to send image/file and normal key value which is being served as normal json later. I am using the form-data type of body in postman and a node server.
To handle image i am using multer on my node server.
But what makes issue is when i try to send the nested json and image together.
I am not able to do this thing.
Everything is fine but this is how the nested json is logging in the terminal :-
Please! Any help would be great to get the nested data object also in actual json format but not like this string as shown in terminal photo.
JSON can't contain binary data. What you're asking isn't directly possible.
The ideal thing to do is a multipart request, which is what you're getting in your first example. Note that one of those parts could be JSON, and you can just reference the other part by name, or with some other identifier.
The wrong way to do this is to base64 encode the data and put it in your JSON. If you do this, you'll get what you're asking for at the expense of 33% overhead in file size, wasted CPU and memory on each end for encoding/decoding, and significant waste in memory for your JSON parser which now has to chew through all of this extra data.
An alternative is to use a format that supports binary data, like CBOR. CBOR works in browsers, is streamable, supports all of the types of JSON and then some, is extensible, and standardized.
One solution is to split image upload and record upload into two separate services and call in UI one after the other.
It may be late but I faced the same issue, what I did was,
1: send data through form-data body and had 2 parameters.
file (the file which we want to send on backend using file field)
data (a string in json format using the text field in form-data).
example:
data : {
"words":500,
"model":0,
"video":true,
"user_id":"user1"
}
I sent this in the request. Remember that when we send using form-data we have 2 separate choices file/text. Since this is text so the whole string is considered a text. So, essentially the data parameter is a string. When I receive this request on backend in django I do the following to get the actual json in json format.
attributes = json.loads(request.data['data'])
This converts the data parameter which was first a string, into a json just as we wanted.
Hope it helps others.
This is just a general question but I can provide an example if necessary. I've been working on a MongoDB/Mongoose, Node.JS, Express, and Handlebars stack app recently and I've ran into some issues with what is being passed from my route to the client side when dealing with Mongoose .find() queries.
I prefer to async await my queries. I have a deeply nested populate query that brings in all the data I need into one object. This is a very simplified breakdown of what I do
async function() { const finalObject = await Model.find({}).populate([*alot more populates*]) }
I pass this into my res.render() route, as so:
res.render('index', { finalObject });
The majority of my experience has been pretty straight forward. I have one issue in a separate stack overflow question about populating the same model twice in one chain (at different levels). I won't address that here.
I decided to do some calculations before passing the object to the route and attach some new values to the properties of the documents. This is where things get weird. I do some calculations and add new properties to documents (these properties are not part of the Schema). When the object is passed to the client-side, those properties I added are available to use (no problem here).
I needed to decode the finalObject into JSON and pass it as JSON as well for an easy use of the data in my client side JavaScript encodedFinalObject = encodeURIComponent(JSON.stringify(finalObject));
I decode it on the client side: clientSideFinalObject = JSON.parse(decodeURIComponent(encodedFinalObject)); and the properties I added are not there!
I'm wondering, how could the new properties I added pass through when I send the object but not pass through when I send the JSON? The decoding is the last thing I do before rendering the page, so it's not a matter of not having my code in the wrong order. I add the properties to the object then decode it.
Some research led me to find out that Model.find({}) does not return a native JavaScript object, but instead a Query. I'm assuming this is a user-defined class data-type that has special behaviors. My assumption is that between the passing from the back-end to the front-end, there is some middle step where the Query does its final processing.
The opposite situation has also occurred. As a solution to this, I decoded the Query finalObject immediately after the Model.find({}) and re-encoded it into a native JavaScript object. After that, I did all my calculations, attached the results as properties to the documents, and sent that new version over as the main finalObject (I still also sent over a separate decoded object for client-side JavaScript use).
This solved the issue of the new properties not being there. They appeared, but this time the decoded then re-encoded finalObject did not have the virtual properties of my Models. The main model has a virtual property that calculates some of it's other properties. It's almost as if that part of the query didn't execute within the .find({}) process. So, when I decoded the finalObject, the virtual properties were never there and were never decoded.
Can anyone explain to me what is going on? Thank you!
I am using node.js as a scripting language and I am also using nunjucks as the template engine. I have a weird situation going where some of the data that I retrieve from the MongoDB database is not being printed into document.
As you see hear these are the values that I want to print to the web page
But when you look at the web page, only some of the data has been printed out and the other information is missing.
I console.logged the data to prove that the values are in the database
A weird thing is that if you write the whole object into the code, like so
It will output all the data in one block. That includes the month, the year, and slug property that I am trying to output to the page. Yet, it only does that if I print out the whole object
I found out what was the problem. In my mongoose schema, I did not have the properties listed like slug, month, or date. So when I tried to retrieve data from those properties it did not output them. So If you are having similar problems make sure the properties are declared in your mongoose schema
I have a jade template where I'm printing out properties from a mongoose object. The object has properties that were populated via mongoose's dbref-like populate feature. When The template renders I get nonsense.
simplified example:
in the route
User.findById(req.params.user, function(err, user){
res.render('training', {training:user.training});
}).populate('training.details');
in the template:
a.training-link(href='/training/#{training.details.id}') ...
in the browser:
...
The first time the page is loaded, where I would expect a regular mongo hexadecimal string (4fac4e5f379cb0a68100015d) I get something like "O >°Cm5�". This only happens on the first page load after a server restart. All subsequent page loads render as expected.
What's happening here and how can I fix it?
Thanks.
This is a bit embarrassing but I figured it out.
There is a logical branch in our app that, for various reasons, causes the code in my question above to run on the second and subsequent loads but not on the first. The other branch, which only runs on the first load, makes a similar query but I hadn't added populate('training.details') to it. I believe the nonsense string O¬N_7°¦� is the result of calling .id on an ObjectId object rather than the populated training.details.
Looks like we need to refactor some code.
I am currently playing around with node.js and MongoDB using the node-mongo-native driver.
I tested a bit around using the Mongo console storing and retrieving JS objects. I figured out, that if I store an object that contains functions/methods the methods and functions will also be stored in the collection. This is interesting since I thought that functions could not be stored in MongoDB (with the exception of the system.js collection, as suggested by the Mongo docs).
Also it will not only store the methods but actually each method and member of the object's entire prototype chain. Besides that I dont like this behaviour and think it's unintuitive I mustn't have it.
I was going to manage users in a Mongo collection. To do this I have a User object containing all of the users methods functioning as a prototype for each instance of an user. The user object itself would only contain the users attributes.
If I store a user in the Mongo collection I only want to store the own properties of the user object. No prototype members and especially no prototype methods. Currently I do not see how to cleanly do this. The options that I figured might work are:
creating a shallow copy using foreach and hasOwnProperty and storing this copy in the collection.
Add a data attribute to each user that contains all the object's attributes and can be stored in the collection.
This just came to my mind writing this: I could also set all the prototypes properties to not enumerable which should prevent them from being stored in the collection.
However, I do have the same issues the other way around: when loading a user from a collection. AFAIK there is no way to change an objects prototype in JavaScript after it was created. And there's also no way to specify a prototype to use when Mongo instantiates objects it retrieved from a collection. So basically I always get objects that inherit from Object using Mongo. As far as I can tell I have 2 options to restore a usable user object from this point on:
Create a fresh object inheriting from User and copying each attribute on the result object to the newly created object. (Compatible to storing mechanisms 1 & 3)
Create a fresh object inheriting from User and storing the result object as a data attribute on the newly created object. (Compatible to storing mechanism 2)
Are my assumptions, especially about the possibility to specify a prototype for query results, correct? What's the right way to do it, and why? I'm surely not the first person struggling to store and resurrect objects in/from MongoDB using node.js.
Currently I would go with the approach 2/2. I don't really like it, but it is the most efficient and the only one that works cleanly with the API. However, I'd much rather hear that actually the API does nothing wrong, but I do for not knowing how to use it correctly. So please, enlighten me :)
I just recently realized, that it actually is possible to change an objects prototype in V8/node. While this is not in the standard it is possible in various browsers and especially in V8/node!
function User(username, email) {
this.username = username;
this.email = email;
}
User.prototype.sendMail = function (subject, text) {
mailer.send(this.email, subject, text);
};
var o = {username: 'LoadeFromMongoDB', email: 'nomail#nomail.no'};
o.__proto__ = User.prototype;
o.sendMail('Hello, MongoDB User!', 'You where loaded from MongoDB, but inherit from User nevertheless! Congratulations!');
This is used all over various modules and plugins - even core modules make use of this technique, allthough it is not ECMAScript standard. So I guess it is safe to use within node.js.
I'm not sure I'm following you question exactly... but fwiw one thing came to mind: Have you checked out the Mongoose ORM? (http://mongoosejs.com/)
It gives you a lot of options when it comes to defining models and methods. In particular "Virtuals" might be of interest (http://mongoosejs.com/docs/virtuals.html).
Anyway, hope it helps some!