Tôi đang sử dụng nút JS với Express và Mongo. Tôi có một chương trình đơn giản trong cài đặt người dùng thông qua các nút radio. Tôi có một nút gửi nhưng tôi không biết làm thế nào để làm cho nó hoạt động từ đó. Tôi có cần một tệp JS mới để gửi nó đến MongoDB không? Tất cả những gì tôi muốn là HTML của tôi để lưu các tùy chọn và lưu trữ chúng vào cơ sở dữ liệu. Tôi bắt đầu đi vào những bước nào?
Ví dụ ở đây: Nếu tôi nhấp vào nam và gửi, tôi sẽ muốn từ nam lưu vào cơ sở dữ liệu.
Male
Female
Đã hỏi ngày 14 tháng 5 năm 2014 lúc 17:16May 14, 2014 at 17:16
1
- Tạo máy chủ.
- Gửi http req.
- Kết nối với MongoDB.
- Chèn dữ liệu trong MongoDB
Đã trả lời ngày 18 tháng 10 năm 2018 lúc 6:47Oct 18, 2018 at 6:47
1
Tìm hiểu cách lưu hoặc chèn các bản ghi hình thức HTML vào bảng cơ sở dữ liệu MongoDB bằng cách sử dụng JS và Mongoose JS. Trong bài viết này, video bạn sẽ tìm hiểu về công cụ xem mẫu MongoDB, Express JS và EJS. Để chèn một bản ghi hoặc tài liệu duy nhất vào MongoDB, bạn cần gọi phương thức Save [] trên phiên bản tài liệu. & nbsp; hàm gọi lại [err, tài liệu] là một đối số tùy chọn để lưu [] phương thức. Việc chèn xảy ra không đồng bộ và bất kỳ hoạt động nào phụ thuộc vào tài liệu được chèn phải xảy ra trong hàm gọi lại cho tính chính xác. & NBSP;save or insert HTML Form records into Mongodb Database table using express js and mongoose js. In this article video you will learn about mongodb, express js and EJS template view engine. To insert a single record or document to MongoDB, you need to call save[] method on document instance. Callback function[err, document] is an optional argument to save[] method. Insertion happens asynchronously and any operations dependent on the inserted document has to happen in callback function for correctness.
Cho lưu & nbsp; Bản ghi biểu mẫu HTML cho MongoDB, bạn cần tạo biểu mẫu HTML trong tệp thư mục xem và xác định Post & nbsp; tuyến đường trong tệp tuyến & nbsp của bạn; Sao chép và dán bên dưới mã biểu mẫu HTML và Mã phương thức bài viết:POST route in your index.js route file. Copy and Paste below html form code and route post method code :
Tạo tệp index.ejs có biểu mẫu HTML:
Tạo tệp index.js trong thư mục xem và sao chép và dán mã bên dưới.Views Directory and copy and paste below code.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;div class="container">
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;Employee Details Form & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
Error:Error:
NameName
EmailEmail
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;& nbsp; & nbsp; & nbsp; loại nhân viênEmployee Type
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;select class="form-control"name="emptype"> & nbsp; & nbsp;Hàng giờHourly & nbsp; & nbsp;đã sửaFixed & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;/select>
& nbsp; & nbsp;Hàng giờHourly Rate
& nbsp; & nbsp;đã sửaTotal Hour
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;& nbsp; & nbsp; & nbsp; tỷ lệ hàng giờ
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
Xác định bộ định tuyến.post [Hồi/] trong tệp index.js/div>
|
Xác định bộ định tuyến.post [Hồi/] trong tệp index.js
Sao chép và dán mã dưới đây trong tệp index.js của thư mục tuyến đường.index.js file of routes folder.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | constmongoose=require['mongoose'];mongoose=require['mongoose']; mongoose.connect['mongodb://localhost:27017/employee',{useNewUrlParser:true}];.connect['mongodb://localhost:27017/employee',{useNewUrlParser:true}]; varconn=mongoose.Collection; conn=mongoose.Collection; varemployeeSchema=newmongoose.Schema[{employeeSchema=newmongoose.Schema[{ name:String,:String, email:String,: String, etype:String,:String, hourlyrate:Number,:Number, totalHour:Number,:Number, total:Number,:Number, }];]; varempModel=mongoose.model['Employee',employeeSchema]; empModel=mongoose.model['Employee',employeeSchema]; varemployee=empModel.find[{}];employee=empModel.find[{}]; router.post['/',function[req,res,next]{.post['/', function[req,res,next]{ varempDetails=newempModel[{varempDetails=newempModel[{ name:req.body.uname,name: req.body.uname, email:req.body.email,email:req.body.email, etype:req.body.emptype,etype:req.body.emptype, hourlyrate:req.body.hrlyrate,hourlyrate: req.body.hrlyrate, totalHour:req.body.ttlhr,totalHour:req.body.ttlhr, total:parseInt[req.body.hrlyrate]*parseInt[req.body.ttlhr],total: parseInt[req.body.hrlyrate]*parseInt[req.body.ttlhr], }];}]; empDetails.save[function[err,req1]{empDetails.save[function[err,req1]{ if[err]throwerr;if[err]throwerr; employee.exec[function[err,data]{employee.exec[function[err,data]{ if[err]throwerr;if[err] throwerr; & nbsp;res.render['index',{title:'Employee Records',records:data,success:'Record Inserted Successfully'}]; }];}]; }]}] }];]; |
& nbsp;
Bạn cần xem các hướng dẫn video bên dưới để có được kiến thức đầy đủ về mã trên:
Nếu bạn thích video của chúng tôi hoặc nếu bạn nghĩ rằng video này hữu ích cho bạn, hãy xem và đăng ký kênh YouTube của chúng tôi để cập nhật video mới nhất. Vì vậy, don quên quên nhấp vào biểu tượng Bell sau khi đăng ký kênh của chúng tôi.
//www.facebook.com/tutorialswebsite
//twitter.com/techwebsitetrix
Theo dõi hướng dẫn:
Kết xuất các bản ghi dữ liệu MongoDB trong bảng HTML với EJS Xem Mẫu động cơ bằng cách sử dụng Express JS và Mongoose JS
Lọc hồ sơ từ cơ sở dữ liệu với mẫu bộ lọc HTML bằng cách sử dụng Express JS và Mongoosejs is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co