2015-12-22 83 views
1

我正在使用MEAN堆栈。我做了登录/注册和个人资料页面。我将用户ID保存在cookie中,现在我想在服务器中发送我的ID。如何从客户端发送cookie到服务器?

我得到我的ID从饼干这样的:

userId = $cookies.get('userId') 

我怎样才能将这个信息发送到服务器?我必须在DB中找到这样的信息:

var userId = "receivedId"; 
User.UserModel.findOne({ _id: userId }, function(err, data) { 
    var User = {_id: data._id, user: data.user, date: data.date}; 
res.send(User); //I know how to send from server to client. 
}); 
+0

所有网络浏览器都会自动将当前网域的所有Cookie包含到所有http请求中,因此您的服务器已经收到它。 –

+0

但我写了console.log(req.body);并且结果未定义:/ –

+0

请检查您的Web服务器文档。如果您使用'express',则必须先配置[cookie-parser](https://www.npmjs.com/package/cookie-parser)中间件。之后,您可以使用'req.cookies'访问客户端Cookie。 –

回答

0

请检查您的Web服务器文档。如果您使用express,则必须先配置cookie解析器中间件。之后,您可以使用req.cookies访问客户端Cookie。 - 狮子座Beschastny

笔者的答案是:狮子座Beschastny

谢谢。有用!

相关问题