Node.js 使用nodejs和mongodb获取登录用户id
我正在尝试使用下面的代码获取登录名Node.js 使用nodejs和mongodb获取登录用户id,node.js,mongodb,Node.js,Mongodb,我正在尝试使用下面的代码获取登录名userId,但它总是只在索引0(零)的位置显示userId api.js 在上面的代码中,我从users集合中获取userId,并将信息插入proInfo集合,插入时始终插入第一个用户的userId,即索引0位置中的用户。但是我想要登录人的用户id。如何解决这个问题请有人帮帮我。它会解决的。这是因为您没有向findOne函数提供查询文档。另外,我认为没有必要从users集合中查找用户,因为您只是在使用用户的\u id字段。因此,您可以简单地执行以下操作: va
userId
,但它总是只在索引0(零)的位置显示userId
api.js
在上面的代码中,我从
users
集合中获取userId
,并将信息插入proInfo
集合,插入时始终插入第一个用户的userId
,即索引0位置中的用户。但是我想要登录人的用户id
。如何解决这个问题请有人帮帮我。它会解决的。这是因为您没有向findOne
函数提供查询文档。另外,我认为没有必要从users
集合中查找用户,因为您只是在使用用户的\u id
字段。因此,您可以简单地执行以下操作:
var insertDocument = function(db, callback) {
var record = {
"Product_Name": json.Product_Name,
"Brand": json.Brand,
"Color": json.Color,
"Image": json.Image,
"Price": json.Price,
"Rating": json.Rating,
"Description": json.Description,
"Category": json.Category,
"Url": urla,
"userId":userIdYouGetFromRequest,
};
db.collection('proInfo').insertOne( record, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the proInfo collection.");
callback(result);
}); };
如果希望在proInfo集合中插入其他与用户相关的字段,请使用查询文档编辑对findOne
方法的调用。大概是这样的:
var insertDocument = function(db, callback) {
db.collection('users').findOne({_id: userIdYouGetFromRequest}, function (err, user) {
if (err) return callback(err);
var record = {
"Product_Name": json.Product_Name,
"Brand": json.Brand,
"Color": json.Color,
"Image": json.Image,
"Price": json.Price,
"Rating": json.Rating,
"Description": json.Description,
"Category": json.Category,
"Url": urla,
"userId":user._id,
};
db.collection('proInfo').insertOne( record, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the proInfo collection.");
callback(result);
});
});
};
注意:必须从前端将用户idYouGetFromRequest
传入请求正文。通常findOne()函数接受一个查询对象
,该对象用于标识集合中的文档。由于您没有提供任何查询对象来标识文档,findOne将返回它在集合中找到的第一个文档。阅读MongoDB文档:
注意:请避免在NodeJS中使用回调进行编程,并使用ES6中的承诺功能支持。它将简化异步编码并最大限度地防止错误
如果我是你,我会将你的代码修改为:
var insertDocument = function (db, callback) {
var queryObj = {
"userId": user._id
};
return db.collection('users').findOne(queryObj)
.then(function (user) {
//If the find query was successful, this function will be
//called with the document returned.
var record = {
"Product_Name": json.Product_Name,
"Brand": json.Brand,
"Color": json.Color,
"Image": json.Image,
"Price": json.Price,
"Rating": json.Rating,
"Description": json.Description,
"Category": json.Category,
"Url": urla,
"userId": user._id
};
return db.collection('proInfo').insertOne(record)
.then(function (result) {
console.log("Inserted a document into the proInfo collection.");
return result;
},
//Handle error for second DB call.
//If db call to insert doc fails, promise will be rejected
function(err){
assert.equal(err, null);
}
);
},
//Handle error for first DB call.
//If db call to find users fails, promise will be rejected
function(err){
return Promise.reject(err);
});
};
在尝试此代码之前,请先了解承诺。这是一个社区广泛采用的功能
var insertDocument = function (db, callback) {
var queryObj = {
"userId": user._id
};
return db.collection('users').findOne(queryObj)
.then(function (user) {
//If the find query was successful, this function will be
//called with the document returned.
var record = {
"Product_Name": json.Product_Name,
"Brand": json.Brand,
"Color": json.Color,
"Image": json.Image,
"Price": json.Price,
"Rating": json.Rating,
"Description": json.Description,
"Category": json.Category,
"Url": urla,
"userId": user._id
};
return db.collection('proInfo').insertOne(record)
.then(function (result) {
console.log("Inserted a document into the proInfo collection.");
return result;
},
//Handle error for second DB call.
//If db call to insert doc fails, promise will be rejected
function(err){
assert.equal(err, null);
}
);
},
//Handle error for first DB call.
//If db call to find users fails, promise will be rejected
function(err){
return Promise.reject(err);
});
};