从node.js向mongoDB数据库中插入数据
Insert data in mongoDB database from node.js
上下文:想要从node.js向mongoDB数据库插入数据
问题声明:我试图在mongoDB数据库中插入数据,但抛出了一个错误。找不到。
当前输出:引用错误
附加代码:
filter.js
var server = require('http'),
express = require('express'),
http = require('http'),
fs = require('fs');
filter = express(),
io = require('socket.io'),
mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/filter_CheckBoxSchema', function(err){
if(err){
console.log(err);
} else{
console.log('Connected to mongodb!');
}
});
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8000);
var filter_CheckBoxSchema = mongoose.Schema({
name: String,
type: Boolean,
created: {type: Date, default: Date.now}
});
var Filter = mongoose.model('Store', filter_CheckBoxSchema);
fs.readFile('./index.html', function (err, html) {
if (err) {
throw err;
}
new Filter({
name: request.body.name,
type: request.body.gender,
}).save(function(err, doc){
if(err)
{
throw err;
}
else
response.send('Successfully inserted!!!');
});
});
index . html
<html>
<head>
<title>Please enter your details</title>
</head>
<body>
<h3>Please enter your details</h3>
<p>Please register below!!!</p>
<form action="filter.js" method="POST">
Name: <input type="text" name="Name" />
<br /><p></p>
Gender:
<br />
<input type="radio" name="gender"/> Male
<br />
<input type="radio" name="gender"/> Female
<p></p>
Interest: (Check all that apply)
<p>
</p>
<input type="checkbox" name="breakfast"/> Breakfast
<br/>
<input type="checkbox" name="Lunch"/> Lunch
<br />
<input type="checkbox" name="Evening Snacks"/> Evening Snacks
<br />
<input type="checkbox" name="Dinner"/> Dinner
<br />
<p></p>
<input type="submit" name="submit" value="Register!!!" />
</form>
</body>
</html>
输出:C:'node'people discovery app>node filter.js
Connected to mongodb!
C:'node'people discovery app'filter.js:152
name: request.body.name,
^
ReferenceError: request is not defined
at C:'node'people discovery app'filter.js:152:9
at fs.js:271:14
at Object.oncomplete (fs.js:107:15)
看来你没有完全掌握javascript的异步特性。传递给函数的变量只存在于该函数的作用域中。请看下面的注释代码:
var express = require('express'),
http = require('http'),
fs = require('fs'),
mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/filter_CheckBoxSchema', function(err){
if(err){
console.log(err);
} else{
console.log('Connected to mongodb!');
}
});
//Lets read our html file only once, at the very beginning when we first start our nodejs process
fs.readFile('./index.html', function (err, html) {
//Now in here there are two variables which are accessible, `err` and `html`
if (err) {
throw err;
}
//Create our schema before starting server
var filter_CheckBoxSchema = mongoose.Schema({
name: String,
type: Boolean,
created: {type: Date, default: Date.now}
});
//And now the model as well
var Filter = mongoose.model('Store', filter_CheckBoxSchema);
//Now lets start our server
http.createServer(function(request, response) {
//The code here is called whenever a new http request is sent to the server
//There are two variables accessible here, one is `request` which contains
//data about the original request, while `response` is an object with methods
//allowing you to respond
//Here we check what kind of method the browser is using, if its POSTing
//data then we create a filter from the body
if (request.method == "POST") {
new Filter({
name: request.body.name,
type: request.body.gender,
}).save(function(err, doc){
if(err)
{
throw err;
}
else {
response.send('Successfully inserted!!!');
}
});
}
else {
//This must have been a GET request, lets just send `html` instead
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}
}).listen(8000);
});
相关文章:
- 通过mongos和javascript插入数据
- HTML5/JavaScript.使用索引数据库时无法插入数据
- 在数据库中插入数据后,无法重定向到 HTML 页面
- 通过 Yii2 表单插入数据时获取空值
- 如何在for循环中的新对象中插入数据
- 如何在HTML中用两个预定义的值绑定选择选项,以在mongodb中插入数据
- jQueryAjax:从php插入数据
- 插入数据并用新数据刷新数据网格后,关闭弹出窗口
- 在用户为下一次输入插入数据后清空文本字段
- 在 jaydata 中的循环中插入数据
- 如何在node.js应用程序中的mongodb中按层次结构顺序插入数据
- 如何在表单输入元素内的数组中插入数据
- 使用 Prepare 语句插入数据不会返回插入的记录
- 如何在编码器中使用具有相同名称的多个输入插入数据
- 将字段与 id 匹配并插入数据 - 流星
- 如何使用javascript或jquery创建tr的克隆并在新行中插入数据
- 在代码点火器上使用 ajax 插入数据
- 如何插入数据 PHP JavaScript MySQL 与多个数组
- 从gsheet向bigquery插入数据
- I'm使用angularjs路线.在日志页面中插入数据后,我希望它在导航到日志列表页面时自动更新