js |
JavaScriptタグが付けられた新着投稿 - Qiita |
webブラウザでのjavaScriptの処理の流れ |
https://qiita.com/t_109609akg/items/355b4efd386db13f778b
|
document |
2022-04-17 18:09:55 |
Ruby |
Rubyタグが付けられた新着投稿 - Qiita |
DockerでRuby on Railsの環境を構築する際のentrypoint.shのコマンド及びコード理解 |
https://qiita.com/zakino123/items/dbdea7bc6a490e863bd2
|
docker |
2022-04-17 18:53:40 |
Docker |
dockerタグが付けられた新着投稿 - Qiita |
🔴CircleCiで、Rails6のRspecのSYSTEMテストを実行する方法【Docker】 |
https://qiita.com/felmy/items/50bebb048eb755bcdd12
|
circleci |
2022-04-17 18:56:17 |
Docker |
dockerタグが付けられた新着投稿 - Qiita |
DockerでRuby on Railsの環境を構築する際のentrypoint.shのコマンド及びコード理解 |
https://qiita.com/zakino123/items/dbdea7bc6a490e863bd2
|
docker |
2022-04-17 18:53:40 |
Docker |
dockerタグが付けられた新着投稿 - Qiita |
WSL 2 に Oracle Linux 8.5 を入れて Docker Desktop を使えるようにした |
https://qiita.com/T4phage25nm/items/c13646b90006a309619d
|
docker |
2022-04-17 18:29:15 |
Git |
Gitタグが付けられた新着投稿 - Qiita |
【備忘録】4月3週目学んだこと(SQL編) |
https://qiita.com/KotaroNagayoshi/items/8aff6074c7d35f87fb46
|
select |
2022-04-17 18:40:09 |
Ruby |
Railsタグが付けられた新着投稿 - Qiita |
🔴CircleCiで、Rails6のRspecのSYSTEMテストを実行する方法【Docker】 |
https://qiita.com/felmy/items/50bebb048eb755bcdd12
|
circleci |
2022-04-17 18:56:17 |
Ruby |
Railsタグが付けられた新着投稿 - Qiita |
DockerでRuby on Railsの環境を構築する際のentrypoint.shのコマンド及びコード理解 |
https://qiita.com/zakino123/items/dbdea7bc6a490e863bd2
|
docker |
2022-04-17 18:53:40 |
海外TECH |
DEV Community |
🗃️ How to use Mongoose with Next.js for MongoDB? |
https://dev.to/maxprogramming/how-to-use-mongoose-with-nextjs-for-mongodb-4966
|
️How to use Mongoose with Next js for MongoDB Hey all Next js is an amazing full stack framework and MongoDB is a great NoSQL database Using them together will make an app super fast and awesome In this post we ll go ahead and set up the Mongoose ODM inside our Next js app to make use of MongoDB So let s see how you can set up Mongoose in a Next js app to connect and interact with your MongoDB database Setting up mongoose and the connection stringIn your Next js project to set up mongoose you simply have to install it as a dependency just as you do with Node js npm i mongooseAfter installing mongoose we ll create a folder called utils in our root and create a new file named connectMongo js file In this file we will export a function that connects us to MongoDB import mongoose from mongoose const connectMongo async gt mongoose connect process env MONGO URI export default connectMongo Also create a file named env local in the root of your project to store the connection URI in your environment variable and hide it from the main code I am using MongoDB locally but you can use MongoDB Atlas also if you wantMONGO URI mongodb localhost mongoose nextjs demo Create a basic mongoose modelNow that mongoose is set up successfully in our Next js project the rest of the work is quite similar to a Node js app I personally like to create a folder called models in the root of my project and create my model files there just like a normal node js app So now we ll create a file named testModel js in our models folder where we ll create our mongoose model import Schema model models from mongoose const testSchema new Schema name String email type String required true unique true const Test models Test model Test testSchema export default Test IMPORTANT Notice how we use models Test and then the logical OR operator and then use the model function by mongoose We do that because we don t want to create a new model every single time we hit an API route in Next js If you don t do that and just go with model Test testSchema you might face an error that would prevent creating updating etc Using mongoose in API routesNow that we have our model created we can use it to see it in action Next js is a full stack framework and so it also provides a node environment where we can run Node js back end code easily and integrate that with the frontend In the pages api folder we can create a file or folder that will ultimately create an API route and we can write back end code in that file and call it as a REST API For this demo I created a folder test and a file add js inside of it which gives the path api test add import connectMongo from utils connectMongo import Test from models testModel param import next NextApiRequest req param import next NextApiResponse res export default async function addTest req res try console log CONNECTING TO MONGO await connectMongo console log CONNECTED TO MONGO console log CREATING DOCUMENT const test await Test create req body console log CREATED DOCUMENT res json test catch error console log error res json error Here we import the connectMongo function and our Test model that we created from the respective files And the big comment I have at the top is JSDoc which can be used to provide autocomplete and typing in the IDE You can omit it if you want Finally the code is simple and straightforward you can use the normal mongoose style code to create a new document By getting the data from req body You can test it from the Thunder Client extension in VS Code Postman or Insomnia Whatever you wish I like to use Thunder Client Create new document from front endNow that we have our back end API created and we have verified that it s working we can quickly write some front end code to make it usable in our app On the homepage inside the index js file I changed the file so that when we click the button a new document will get added to the database import Head from next head import Image from next image import styles from styles Home module css export default function Home const createTest async gt const randomNum Math floor Math random const res await fetch api test add method POST headers Content Type application json body JSON stringify name Test randomNum email test randomNum test com const data await res json console log data return lt div className styles container gt lt Head gt lt title gt Create Next App lt title gt lt meta name description content Generated by create next app gt lt link rel icon href favicon ico gt lt Head gt lt main className styles main gt lt button onClick createTest gt Create Test lt button gt lt h className styles title gt Welcome to lt a href gt Next js lt a gt lt h gt lt p className styles description gt Get started by editing lt code className styles code gt pages index js lt code gt lt p gt lt div className styles grid gt lt div gt lt main gt lt footer className styles footer gt lt a href utm source create next app amp utm medium default template amp utm campaign create next app target blank rel noopener noreferrer gt Powered by lt span className styles logo gt lt Image src vercel svg alt Vercel Logo width height gt lt span gt lt a gt lt footer gt lt div gt Fetch and display documentsNow it s time to use Next js s biggest feature Server Side Rendering We can use SSR in Next js to easily run back end Node js code as we want and the data can be easily accessible through the props to the page In the index js file itself we ll import the connectMongo and Test again and use them inside the getServerSideProps function that we have to export like this import connectMongo from utils connectMongo import Test from models testModel export const getServerSideProps async gt try console log CONNECTING TO MONGO await connectMongo console log CONNECTED TO MONGO console log FETCHING DOCUMENTS const tests await Test find console log FETCHED DOCUMENTS return props tests JSON parse JSON stringify tests catch error console log error return notFound true In this function we can easily fetch any data we want and it will be done on the server and then we have to return it as props That will be accessible to the page You can read about getServerSideProps on the Next js docsIMPORTANT Make sure to sanitize the tests variable with JSON parse JSON stringify tests because it contains ObjectID from MongoDB instead of a normal string This trick converts it into a string that can be passed in the return object That s it After we re done fetching the data we can easily display it by accessing it through the props on our page and we can use it however we want In this case we ll map over that data to output every document like soexport default function Home tests return lt div className styles grid gt tests map test gt lt a href key test id className styles card gt lt h gt test name amp rarr lt h gt lt p gt test email lt p gt lt a gt lt div gt Finally this is what our page looks like I have explained everything deeply in the YouTube tutorial below I hope this post helped you successfully set up Mongoose in your Next js app If it did please leave a like Comment down your thoughts There is always room for improvement so let me know your suggestions on this project Connect with me on my YouTube channel and my Twitter Thanks for reading |
2022-04-17 09:08:47 |
海外ニュース |
Japan Times latest articles |
India is stalling WHO’s efforts to make global COVID death toll public |
https://www.japantimes.co.jp/news/2022/04/17/world/india-global-covid-death-count/
|
India is stalling WHO s efforts to make global COVID death toll publicMore than one third of an additional million deaths are estimated to have occurred in India but the country is not alone in undercounting the |
2022-04-17 18:13:57 |
海外ニュース |
Japan Times latest articles |
The elusive politics of Elon Musk |
https://www.japantimes.co.jp/news/2022/04/17/business/elon-musk-politics-twitter/
|
binary |
2022-04-17 18:08:28 |
ニュース |
BBC News - Home |
UK's Rwanda asylum plan the 'opposite of nature of God' - Welby |
https://www.bbc.co.uk/news/uk-61130841?at_medium=RSS&at_campaign=KARANGA
|
africa |
2022-04-17 09:17:01 |
ニュース |
BBC News - Home |
Ukraine war: Trucks stuck at Poland-Belarus border as EU sanctions deadline passes |
https://www.bbc.co.uk/news/world-europe-61133439?at_medium=RSS&at_campaign=KARANGA
|
belarus |
2022-04-17 09:44:39 |
ニュース |
BBC News - Home |
Boris Johnson and Prince Charles remember war in Ukraine in Easter messages |
https://www.bbc.co.uk/news/uk-61129126?at_medium=RSS&at_campaign=KARANGA
|
boris |
2022-04-17 09:10:51 |
ニュース |
BBC News - Home |
Easter: Snowdonia mountain path covered in human faeces - guide |
https://www.bbc.co.uk/news/uk-wales-61128854?at_medium=RSS&at_campaign=KARANGA
|
railway |
2022-04-17 09:17:54 |
ニュース |
BBC News - Home |
Why has Russia invaded Ukraine and what does Putin want? |
https://www.bbc.co.uk/news/world-europe-56720589?at_medium=RSS&at_campaign=KARANGA
|
ukraine |
2022-04-17 09:13:22 |
北海道 |
北海道新聞 |
ロ0―1日(17日) 日本ハム、1安打で競り勝つ |
https://www.hokkaido-np.co.jp/article/670631/
|
日本ハム |
2022-04-17 18:30:55 |
北海道 |
北海道新聞 |
北海道内2113人感染 5日連続2千人超 1人死亡 新型コロナ |
https://www.hokkaido-np.co.jp/article/670589/
|
北海道内 |
2022-04-17 18:28:50 |
北海道 |
北海道新聞 |
83歳堀江謙一さん、ハワイ通過 太平洋横断へ映像公開 |
https://www.hokkaido-np.co.jp/article/670610/
|
堀江謙一 |
2022-04-17 18:12:20 |
北海道 |
北海道新聞 |
神1―3巨(17日) ウォーカーが逆転3ラン |
https://www.hokkaido-np.co.jp/article/670630/
|
逆転 |
2022-04-17 18:19:00 |
北海道 |
北海道新聞 |
400m障害は大会新で黒川V 日本学生個人陸上最終日 |
https://www.hokkaido-np.co.jp/article/670629/
|
神奈川県 |
2022-04-17 18:19:00 |
北海道 |
北海道新聞 |
海外から到着の77人感染 14人に症状 |
https://www.hokkaido-np.co.jp/article/670628/
|
厚生労働省 |
2022-04-17 18:19:00 |
北海道 |
北海道新聞 |
北海道弁アナウンスでほっこり 小樽出身JAL機長が話題 |
https://www.hokkaido-np.co.jp/article/670618/
|
北海道弁 |
2022-04-17 18:10:00 |
北海道 |
北海道新聞 |
東京SGがプレーオフ一番乗り ラグビーのリーグワン |
https://www.hokkaido-np.co.jp/article/670626/
|
一番乗り |
2022-04-17 18:09:00 |
コメント
コメントを投稿