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
Related
Ok, what im hoping is possible here is that given an input document with some json in it, I was considering creating a schema document using this json and then output it to a string which I can then use as a reference for future validation. (we have a LOT of json passed around in our software, so creating a schema from what we have will allow me to catch in CI when changes have been made so we can review for privacy violations).
I realise this isn't at all what the schema validator tools are for. but it looks like they do create internally the schema, does anyone have any idea how I might be able to do this?
Thanks
I am new to NodeJS, MongoDB and express.
I am able to insert, delete and update data but I dont really know how to work with it.
I want to choose some data(e.g. from a dropdown form) and then pass that data to some functions and calculate results. How is it possible to show the results to the client in the webpage(e.g. in a table)?
For example: I have different JSON objects stored in my database and now I want to select one by the name. Then do some calculations with it and show the results to the client.
Also: Is it possible to tell the Database to only accept valid JSON data?
Yes you can get specified data from the database and it will work a lot better with mongoose. You can define schema in mongoose and you can focus more on the data instead of how your data look like. And you can do validation to accept the valid data.
I'm pretty new to MongoDB and best practices. I'm using Node full stack JS.
I created a form which spans multiple screens. This saves to a mongoose.model Schema and creates a document when the user submits the form.
I've been given the requirement to allow the user to save the form when part way through. The problem with trying to save to the existing Schema is I get a duplicate id reference error as I'm saving multiple fields as null which already exist in the Collection. Plus I'm thinking this is a waste of memory.
The answer is very simple it just took me some circular thinking to get to it. I check to see if properties are undefined before setting them. If they are undefined they are an empty value rather than null i.e. {} for an Object. Mongoose is ok with that, it just doesn't like null values (makes sense).
I want to have default values for certain fields in my mongoose models. The trick is I don't want to store these values in the database, but what to add them when the model is initialized.
Please help.
You can define an 'init' middleware function for the schema which runs when a model instance is loaded from the database. That should let you manipulate the instance to add your defaults as needed.
Also see this related question for more details as the docs are pretty spare on this.
Maybe a json file?
Using a require('path/to/json' ) you can acces to it and get the values.
Im actually doing something like that, but using the same JSON SCHEMA wich is build for validation purpose.
In past with my PHP / Rails - MYSQL apps I've used the unique ID of a table record to keep track of a record in an html file.
So I'd keep track of how to delete a record shown like this (15 being the ID of the record):
Delete this record
So now I'm using MongoDB. I've tried the same method but the objectID ._id attribute seems to be a loooong byte string that I can't use conveniently.
What's the most sensible way of binding a link in the view to a record (for deletion, or other purposes or whatever)?
If the answer is to create a new id that's unique for each document in the collection, then what's the best way to generate those unique id's?
Thank you.
You could use a counter instead of the ObjectID
But this could create a problem when inserting a new document after you deleted a previous one.
See this blog post for more detail info on Sequential unique identifiers with Node.js and MongoDB.
Or you could use the timestamp part of the ObjectID:
objectId.getTimestamp().toString()
See the node objectid docs