【发布时间】:2021-10-31 00:44:36
【问题描述】:
我正在制作一个基本的博客应用程序,我想在用户单击提交按钮时保存他们的数据。我正在使用 mongoose 在 MongoDB 中保存数据。我写了一些代码,但它不起作用。我是这个网站的新手,我正在努力学习。
我写了一些代码保存到数据库中:
const mongoose = require("mongoose");
const User = require("../models/user");
const db_URI = 'my db_uri in here, idk about security so i didn't write it';
const button = document.getElementById("submit-btn");
mongoose.connect(db_URI)
button.onclick = function() {
var user = new User( { # I made this variable for just test
name:"qwe",
password: "123",
email: "example@hotmail.com"
});
user.save()
};
此代码用于我的前端部分:
<!DOCTYPE html>
<html>
<head>
<title>SIGN UP HERE</title>
</head>
<body>
<form method="POST">
<div>
<label id="name-lbl">Name: </label>
<input type="text" name="nick" placeholder="Your nickname here">
</div>
<div>
<label>E-mail: </label>
<input type="text" name="email" placeholder="Your email here">
</div>
<div>
<label>Password: </label>
<input type="password" name="pwd" placeholder="Your password here">
</div>
<div>
<label>Confirm password: </label>
<input type="password" name="pwd" placeholder="Confirm your password">
</div>
<button type="submit" id="submit-btn">Submit</button>
</form>
<script src="../database/db_processes.js"></script>
</body>
</html>
【问题讨论】:
-
前后端js都不一样;你需要实现一个像 express 这样的服务器来完成。
-
好的。但是如何在不使用任何框架的情况下将用户的输入保存到数据库中呢?
-
你不能;或使用火力;它对此有一些能力,
标签: javascript html mongodb mongoose