Node.js application not rendering mongoDB collection after model.find() - node.js

I'm experiencing an issue in my node application with accessing the contents of a mongoDB collection via mongoose.
this problem only ever happens the very first time my application makes a call to the database collection. what should occur when the user presses the submit button, is that the contents of the collection should be added dynamically to the DOM. However, this does not occur. but when I check the collection via the command line interface, the user submitted data is there, and then when I press the refresh button on the browser (which hits the same route the user was redirected to upon initially submitting the form) the data renders to the page like it should have the first time.
When I noticed this, i tried starting from scratch. I dropped my database, restarted my server, but this time, I used robo3t to create the db and the collection before I started my app and filled out the form data, and viola, when I submit my data and hit save, the information gets saved to the collection AND rendered the the page successfully the first time.
So i went and looked at the different Model methods provided by mongoose and thought maybe I just needed to add a function call to initialize the collection sometime before calling Model.find() but everything i'm looking at tells me that the collection is already (obviously) initialized when you call mongoose.model()
so i'm a little confused about what to do in order to make sure my data gets rendered to the webpage the first time the user submits information, rather than after refreshing.....i've been careful about awaiting all the async functionality but maybe I missed something?
the repo is here in the unlikely case someone wants to clone this and try to recreate the situation. Let me know if there is more information I can provide
https://github.com/Funkh0user/TripCheq-Travel-Assistant
Thank you.

I Solved it. I needed to rearrange some things such that the block of code responsible for doing some computations and then sending the result back to the application was in the callback for model.find()
Thanks!

Related

Node.js: Is there an advantage to populating page data using Socket.io vs res.render(), or vice-versa?

Let's say, hypothetically, I am working on a website which provides live score updates for sporting fixtures.
A script checks an external API for updates every few seconds. If there is a new update, the information is saved to a database, and then pushed out to the user.
When a new user accesses the website, a script queries the database and populates the page with all the information ingested so far.
I am using socket.io to push live updates. However, when someone is accessing the page for the first time, I have a couple of options:
I could use the existing socket.io infrastructure to populate the page
I could request the information when routing the user, pass it into res.render() as an argument and render the data using, for example, Pug.
In this circumstance, my instinct would be to utilise the existing socket.io infrastructure; purely because it would save me writing additional code. However, I am curious to know whether there are any other reasons for, or against, using either approach. For example, would it be more performant to render the data, initially, using one approach or the other?

Managing state changes caused by an API patch

I have a data table that is populated GUI -> API call -> node backend -> loop back -> mongo db. Just displaying data works fine using NGRX state management. I use a modal dialog to edit one of the fields, trigger a submit change action, call the relevant patch API and successfully update the relevant record(s).
In order to reflect this changed record in the GUI. I call the get API when the SUCCESSFUL_SUBMIT action comes. This works fine, but i think it's not the correct way to do things with NGRX and I worry that this will cause maintenance problems in the future.
So my question is, how do i reflect the changed state in the GUI. Please note that I do not wish to display the change in the GUI until i know that the DB patch was successful.
The image shows the table with the console log having just submitted a change to auto archive.
This seems fine to me, but:
if the POST object reflects the GET object and if the server doesn't modify properties, you could update your store without the extra GET
the POST response body could include the object to prevent the extra GET

Session Values Fails to Load on Request but Loads on Next Request

As a newbie to node, I am getting more confused for several days now, and I think I can't withstand the stress anymore. So I think my only way out is to post my challenge here after goggling and attempting any means I can without any positive result.
I am using express with node, together with the express-session and redis to build my app. Meanwhile, I am using Mysql database to store my data, and redis to store sessions. Everything seem to be working normal, but the issue has got to do with my session variables. When I store a value in session, and try's to retrieve it, it won't load, meanwhile, the value is output perfectly in the console. However, when I try to request something else, then the previous session value is displayed. So is like previous sessions gets loaded on current request. I am using handlebars as my template. I just don't know what I am doing wrong, but all the same, this is strange. As I pointed out, previous session value loads on next request or say current request loads previous session values. Any suggestions please?

Persisting and identifying data in nodejs without a database

I am new to node and back end so please excuse me if my question seems dumb.
My use scenario is as follows:
I have a simple UI that will make only one ajax call at a given time. I have a node js backend that will take this call, make a login call to a webservice and use the response data to make another get call. When the call is over I will make a logout call and delete the login response data.
Problem:
The problem is that being single threaded and async and having no database the logout call will invalidated the login data for all the calls coming afterwards or that are in progress. I need a way to persist and encapsulate the data for each call without blocking the IO for each request.
Solution:
The only thing that I thought so far was to save the login data into dynamic created variables (based on the UI caller ID) and delete those vars when the call is over. However this seems like a very error prone solution that might also cause memory leaks.
I do not want to persist the data into a database and could not figure other solution, can you please advise?

hood.ie with couchDB method to refresh pages with DB change

I've started using hood.ie to make a web app.
I wanted to ask is there a convenient way to refresh pages whenever data in the couch db changes?
Thanks.
If you have a function that renders your page, like render, you can do this
hoodie.store.on("change", render)
The "change" event will trigger every time something changes in your data, be it because you called on of the hoodie.store APIs, or because of a changed synced from remote

Resources