gpt4 book ai didi

javascript - queryselector 必须是对象 mongodb

转载 作者:可可西里 更新时间:2023-11-01 10:41:01 27 4
gpt4 key购买 nike

我尝试在数据库中发布数据,但出现此错误 queryselector must be an object mongodb。这是我的代码

模型 var MongoClient = require('mongodb').MongoClient;

exports.register = function(val,cb) {
var email = val['email'];
var password = val['password'];
var user = val['user'];
MongoClient.connect(//url connect to db ,function(err,db){
if(err)
cb(null,{result:false,message:err});
db.collection('user').findOne(email,function(err,user){
if(err)
cb(null,{result:false,message:err});
if(user){
cb(null,{result:false,message:'Email already exists'});
}else{
db.collection('user').insert(user,function(err){
if(err)
cb(null,{result:false,message:err});
cb(null,{result:true,message:'Success'});
})
}
})
});
};

Controller

var express= require('express');
var router = express.Router();
var user = require('../models/register');

router.post('/',function(req,res) {
var val =[];
val['email'] = req.body.email;
val['password'] = req.body.password;
val['user']={
email:req.body.email,
password:req.body.password
}

user.register(val,function(err,data){
if(err)
console.log(err)
res.json(data);
})

});

module.exports = router;

我尝试更改为 val={} 但还是不行。我哪里错了?请帮助我

最佳答案

应该是..

 exports.register = function(val,cb) {
var email = val['email'];
var password = val['password'];
var user = val['user'];
MongoClient.connect(//url connect to db ,function(err,db){
if(err)
cb(null,{result:false,message:err});
db.collection('user').findOne({email:email},function(err,user){
if(err)
cb(null,{result:false,message:err});
if(user){
cb(null,{result:false,message:'Email already exists'});
}else{
db.collection('user').insert(user,function(err){
if(err)
cb(null,{result:false,message:err});
cb(null,{result:true,message:'Success'});
})
}
})
});
};

关于javascript - queryselector 必须是对象 mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44037772/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com