Javascript MongoDB收集中的数据未通过
您好,我正在尝试执行一个小应用程序,将一个数据数组传递给Mongodb,但它没有通过 我正在包括我的索引前端部分Javascript MongoDB收集中的数据未通过,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,您好,我正在尝试执行一个小应用程序,将一个数据数组传递给Mongodb,但它没有通过 我正在包括我的索引前端部分 函数addListItem(标题,listId){ var ul=document.getElementById(listId); var li=document.createElement(“li”); li.className='列表组项'; li.appendChild(document.createTextNode(title)); ul.儿童(li); } 让结果; 函数
函数addListItem(标题,listId){
var ul=document.getElementById(listId);
var li=document.createElement(“li”);
li.className='列表组项';
li.appendChild(document.createTextNode(title));
ul.儿童(li);
}
让结果;
函数后加载(){
var data=JSON.parse(this.responseText);
var name=document.createElement('img');
结果=数据。结果;
//循环浏览项目
results.forEach(项目=>{
addListItem(item.title,“items”);
});
name.src=data.title;
document.body.appendChild(名称);
}
函数afterClick(){
//将目标更改为焦点搜索
var terms=document.getElementById(“搜索”).value.split(“”).join(‘+’);
var request=new XMLHttpRequest();
请求。addEventListener(“加载”,后加载);
request.open('GET','https://api.themoviedb.org/3/search/movie?api_key=8318c431b4fc8a2c4762bf2a52c351ee&query="条款",;
request.send();
}
按钮。addEventListener(“单击”,单击后);
const submitBtn=document.querySelector('input[value=“Submit”]”);
const favMovie=document.querySelector('form>input');
submitBtn.addEventListener('click',函数(e){
e、 预防默认值();
常量favMovieName=favMovie.value;
如果(favMovieName.length>0){
const filteredFavMovies=results.filter({title})=>title.toLowerCase().includes(favMovieName.toLowerCase());
console.log(filteredFavMovies);
//向mongodb传递数据
var xmlhttp=new XMLHttpRequest();//新的HttpRequest实例
xmlhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
document.getElementById(“结果”).innerHTML=
这个.responseText;
}
};
open(“POST”http://localhost:3000");
setRequestHeader(“内容类型”,“应用程序/json;字符集=UTF-8”);
send(JSON.stringify(filteredFavMovies));
}
});代码>
看电影123
添加Fav电影
列表
尝试一下:
app.post('/', function(req, res) {
db.collection('documents').insertOne({body: req.body}, function (err, result) {
if (err)
res.send('Error');
else
res.send('Success');
});
});
同时移动app.listen(3000)代码>外部mongodb连接功能(到文件末尾)尝试一下:
app.post('/', function(req, res) {
db.collection('documents').insertOne({body: req.body}, function (err, result) {
if (err)
res.send('Error');
else
res.send('Success');
});
});
同时移动app.listen(3000)代码>外部mongodb连接功能(到文件末尾)MongoClient
从更新的mongodb
连接器不返回数据库
参考。它返回client
let connection;
//Establish Connection
MongoClient.connect('mongodb://localhost:27017', { useNewUrlParser: true }, function (err, client) {
if (err)
throw err
else {
connection = client;
console.log('Connected to MongoDB');
//Start app only after connection is ready
app.listen(3000);
}
});
然后在你的路线上:
app.post('/', function(req, res) {
// Insert JSON straight into MongoDB
connection.db('myproject').collection('documents').insert(req.body, function (err, result) {
if (err)
res.send('Error');
else
res.send('Success');
});
});
更新的mongodb
中的MongoClient
连接器不返回数据库
参考。它返回client
let connection;
//Establish Connection
MongoClient.connect('mongodb://localhost:27017', { useNewUrlParser: true }, function (err, client) {
if (err)
throw err
else {
connection = client;
console.log('Connected to MongoDB');
//Start app only after connection is ready
app.listen(3000);
}
});
然后在你的路线上:
app.post('/', function(req, res) {
// Insert JSON straight into MongoDB
connection.db('myproject').collection('documents').insert(req.body, function (err, result) {
if (err)
res.send('Error');
else
res.send('Success');
});
});
请同时发布错误堆栈。@DEVCNN对不起,什么是错误堆栈?您在终端或浏览器控制台中得到的错误。@DEVCNN我已在我的问题检查中发布:-(索引):87 posthttp://localhost:3000/
和获取http://localhost:3000/undefined 404(未找到)
位于索引文件xmlhttp.send(JSON.stringify(filteredFavMovies))
这些错误是从哪里来的?发布一个屏幕截图。如果错误在终端中,发布该错误的屏幕截图。如果错误在浏览器中,请按F12打开控制台并发布该错误的屏幕截图。请同时发布错误堆栈。@DEVCNN对不起,什么是错误堆栈?您在终端或浏览器中获得的错误nsole@DEVCNN我已在我的问题检查中发布:-(索引):87 POSThttp://localhost:3000/
和获取http://localhost:3000/undefined 404(未找到)
在索引文件xmlhttp.send(JSON.stringify(filteredFavMovies))中
这些错误是从哪里来的?发布一个屏幕截图。如果错误在终端中,发布一个屏幕截图。如果错误在浏览器中,请按F12打开控制台并发布一个屏幕截图。没有通过是的,您仍然收到错误吗?是的,与前面提到的错误相同。没有通过是的,您仍然收到错误吗?是的es与前面提到的相同错误。获取相同错误。我如何检查数据是否从我的前端传递?正如您可以看到的,正在控制台上创建数组。我正在文档中传递该数组。由于相同的错误,您是否仍然获得db。收集不是函数
或其他什么,另一个错误404?请在路由app.use((req,res,next)=>{console.log(req.url);next()})
和在您的路径console.log(req.body)
中,我在MongoClient.connect上包含了{useNewUrlParser:true}
,现在没有500个服务器错误,但是数据没有在mongodb中传递。我可以传递数组吗[]在文档中?只需将所有代码放入回调中。得到相同的错误。我如何检查数据是否从我的前端传递?正如您所看到的,正在控制台上创建一个数组。我在文档中传递该数组。由于相同的错误,您仍然得到db。collection不是函数
或其他什么,另一个错误是404?在路由app.use((req,res,next)=>{console.log(req.url);next()})
之前挂载它,并且在开始时的路由console.log(req.body)
中包含{useNewUrlParser:true}MongoClient.connect上的
现在没有500服务器错误,但是数据没有在mongodb中传递。我可以在文档中传递数组[]吗?只需将所有代码放在回调中即可。