Store one schema in multiple connections with Mongoose [closed] - node.js

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a User Schema and when i do a POST request i want the request to specify in which database the object will be stored.
I am trying to rewrite a Laravel API in which i just do
$user->setConnection("databaseName")->save();
I currently have the schema on a different .js file

Mongoose has an API you can use to switch from one database to the other:
Connection.prototype.useDb()
More details about the method in the docs.
Calling that method returns a new mongoose connection with which you can use to perform database queries on the selected database. Using multiple connections in mongoose can be tricky but there is a very good doc on handling multiple connections here.

Related

Internal Server Error when calling a lambda URL function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this question
I was given a lambda function app to work on for the first time. I noticed that on postman and on browser, calling the base URL gives an Internal Server Error response with a 502 - Bad Gateway status code.
I honestly do not know why this happens. Could anyone please point me in the right direction in fixing the issue?
URL: https://kngynmvk5pqcerv2wbdb5mmiuu0jfmjr.lambda-url.us-east-2.on.aws/

What is the best method for Backend and Frontend communication (incl. SessionID)? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
I have a small project running and I question myself, how to do it. I host on two server. One is for Backend and one for Frontend. The Backend-Server is running on NodeJS and as DB MongoDB and I already have my own small API to communicate with it. On the Frontend-Server I am using React. My Question now: What is the best way to make a SessionID on the Frontend and Send it to the Backend-Server over the Frontend-Server?
For example SessionID3 should get displayed a bike on his site and SessionID2 a car (both informations are stored on the Backend-Server on the DB).
I look forward to any replies, thanks!
Most Jamstack setups have an API server that serves content in the form of json/raw (like Github's developer API)in which Nodejs and JavaScript has a built in feature which allows the quick conversion to a JSON object with references in which you can read about that on MDN.
Diagram of Jamstacking with React & Vue
^ since it is actually the client sending the request and doing most of the work and not the front-end server, you can use universal-cookie to set or get a session ID permanently, parse it into JSON and send with the POST request to the Back-end API. You could also do this with Math.random(min, max) as well of you just want a random string of numbers to be sent.
Axios is asynchronous so you want to use async/await or .then() with it so that it works correctly. You can view the official docs at https://axios-http.com/docs/intro. The value returned by Axios is the response in which you will want to parse the "body" the header returned so it can be used.
Another alternative to get the browser session id (which is erased after the browser is closed) is a module called react-session-hook
In my opinion, for what you are trying to do, adding another server that relays the message is not optimal as that adds another sequential connection that makes things take twice as long as it would otherwise. In other-words, this is not necessary. The only time you do this is if the keys to access the API are sensitive meaning they contain important information and musts be hidden.

Need code sugg. for my spec to create small admin application [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
My Spec : Login, Logout, dashboard, I need to render 150 inputs in single form, need to download pdf file for saved inputs. And each inputs need to save input blur action and also form submission.
The existing application has build nodeJs with expressJs with jade template for rendering. Backend(mySql) connection has builded laravel (lumen).
FrontEnd - NodeJS, ExpressJs, Jade template
Backend - Laravel Lumen
Provide some suggestion to best language for my requirement to code.
Had some suggestion like
Laravel + VueJs
NodeJs + ExpressJs + ReactJs
NodeJs + ExpressJs
Thanks in advance
You can create REST API using NodeJS and Express framework. If it just uploading file and auth you can use middleware function and the library you can use like multer and jsonwebtoken for uploading and user validation.. i prefer hbs instead of jade because it HTML based.
I hope this helps

Image to base64 and upload to MongoDB in MERN [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Hello and thanks to everyone for helping.
So I would like help with my project.
So what I would like to happen is get an image from react after posted,
then convert that to base64 so I can post to mongo with mongoose,
and finally, be able to display that but in the process of all of this,
I don't wanna use local saving or in any clouds.
Does anyone know how to accomplish that?
Please share you ideas.
-StayHome
React: Uploading
For the Base64 Conversion, You could:
You could use plain javascript to accomplish that. Refer more here
Even better, use the advantage of NPMs for React. Use a library someone has already built in. This documentation looks awesome for it. Here's the demo for it.
Once you have converted that to base64. I presume you will be storing the details in the react state, you could now do some AJAX request to your server.
Express And Node: Receiving Data Most of this will be boilerplate code until you get to the part where you have to write the APIs. For this portion, you will need to create an API on a specific route that you will specify that will accept POST method.
Three important folders to take a look into:
Routes
Controllers
Models
Routes will be your entry point for APIs, and will use functions that are defined in the controllers. These controllers are made using mongoose functions that will be based on the models that you will create. The controllers you probably will need for this scenario is CREATE and READ.
Mongoose and MongoDB
Connect those to MongoDB. There's a lot of resource for this such as this.
Express and Node: Giving Data
Once you have finished the database models and controllers. You will have a controller for getting specific data in the database. Now you can create a route that will serve this API, usually in GET method.
React: Receiving the Stored Image
In the page that you require the image, you could perform a AJAX request in componentDidMount and store the data in the state.
Using HTML and react, you could create this. More info here
<div>
<p>Taken from wikpedia</p>
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
</div>

Bot Framework - Can't get response from Skype [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have created a bot application using Microsoft Bot Framework.
It's working locally fine, but when I add it to Skype we could not get the response from Skype.
My endpoint url is:
http://balajiamishra-001-site1.htempurl.com
At least two things are incorrect with your endpoint URL and probably you get errors because of that.
Endpoint URL should be HTTPS instead of HTTP as HTTP is not allowed.
Second, you should always keep in mind adding this to the end of your endpoint url:
/api/messages
otherwise you might get mad errors.
So your endpoint URL should look like this:
https://balajiamishra-001-site1.htempurl.com/api/messages
Additional notes:
Your URL looks odd. If I got it right that it's some temporary url then instead consider using ngrok for exposing bot your bot. It's easier and also gives you https with valid certificate.
More details about configuring bot can be found here.

Resources