在我的node.js代码中,当我将带有ID的客户附加到我的购物车时,但它是这样追加
"customer" : ObjectId("5755251e4e2210ce2f953407")
有没有办法得到这样的
"customer" : "5755251e4e2210ce2f953407"
function login(req, res, next) {
db.users.findOne({
'email': req.body.email
}, function(err, user) {
if (err) return next(err);
if (!user) {
return res.status(401).send("user not found");
}
if (lib.getSaltedPassword(req.body.password) != user.password) {
return res.status(401).send("wrong password");
}
req.session.user = user;
if (req.session.cart) {
req.session.cart.forEach(function(data) {
data.customer = user._id
})
db.carts.insert(req.session.cart, function(err, user_cart) {
if (err) return next(err);
})
}
}
res.send({
message: 'logged in',
user_id: user._id
});
});
}
答案 0 :(得分:1)
只需添加.toString(),如下所示:
function login(req, res, next) {
db.users.findOne({
'email': req.body.email
}, function(err, user) {
if (err) return next(err);
if (!user) {
return res.status(401).send("user not found");
}
if (lib.getSaltedPassword(req.body.password) != user.password) {
return res.status(401).send("wrong password");
}
req.session.user = user;
if (req.session.cart) {
req.session.cart.forEach(function(data) {
data.customer = user._id.toString();
})
db.carts.insert(req.session.cart, function(err, user_cart) {
if (err) return next(err);
})
}
}
res.send({
message: 'logged in',
user_id: user._id
});
});
}