搜索一个对象数组以匹配来自MongoDB的objectId。

huangapple go评论139阅读模式
英文:

Search an array of objects to match objectId from mongodb

问题

以下是您要翻译的内容:

"我正在尝试通过包含嵌套对象ID的键来搜索对象数组以进行填充。

我的对象


  1. {
  2. service: 'user',
  3. isModerator: true,
  4. isAdmin: true,
  5. carts: [
  6. {
  7. _id: 5e1344dcd4c94a1554ae0191,
  8. qty: 1,
  9. product: 5e09e4e0fcda6f268cefef3f,
  10. user: 5e0dda6d6853702038da60f0,
  11. expireAt: 2020-01-06T14:31:56.708Z,
  12. __v: 0
  13. },
  14. {
  15. _id: 5e13455306a54b31fc71b371,
  16. qty: 1,
  17. product: 5e09e507fcda6f268cefef40,// object ID
  18. user: 5e0dda6d6853702038da60f0,
  19. expireAt: 2020-01-06T14:33:55.573Z,
  20. __v: 0
  21. },
  22. ],

我想要匹配 carts 数组,以确定用户是否正在添加包含在其中的产品,以便不重复添加,而是增加现有产品的数量。

我的代码

  1. const itemId = req.body._id;
  2. const userId = req.user._id;
  3. const { qty } = req.body;
  4. try {
  5. const producto = await Product.findById(itemId);
  6. const user = await User.findById(userId).populate({
  7. path: 'carts',
  8. });
  9. const result = user.carts.find((o) => {
  10. console.log(typeof o.product) // 返回对象
  11. console.log(typeof producto._id); // 返回对象
  12. return o.product === producto._id
  13. });
  14. console.log(result); // 返回 undefined
  15. if (result !== undefined) {
  16. const foundCart = await Cart.findById(result._id);
  17. foundCart.qty += qty;
  18. await foundCart.save();
  19. return res.json({ message: 1 });
  20. }
  21. const newCart = new Cart({
  22. qty,
  23. product: producto,
  24. user,
  25. });
  26. const cart = await newCart.save();
  27. user.carts.push(cart);
  28. await user.save();
  29. return res.json({ message: 1 });
  30. } catch (error) {
  31. return console.log(error);
  32. }
英文:

I'm trying to search an array of objects by a key that contains nested object id for populating.

My object


  1. {
  2. service: 'user',
  3. isModerator: true,
  4. isAdmin: true,
  5. carts: [
  6. {
  7. _id: 5e1344dcd4c94a1554ae0191,
  8. qty: 1,
  9. product: 5e09e4e0fcda6f268cefef3f,
  10. user: 5e0dda6d6853702038da60f0,
  11. expireAt: 2020-01-06T14:31:56.708Z,
  12. __v: 0
  13. },
  14. {
  15. _id: 5e13455306a54b31fc71b371,
  16. qty: 1,
  17. product: 5e09e507fcda6f268cefef40,// object ID
  18. user: 5e0dda6d6853702038da60f0,
  19. expireAt: 2020-01-06T14:33:55.573Z,
  20. __v: 0
  21. },
  22. ],

I want to match if carts array, contains cart with a product that user is adding, to not add it the second time but instead increment qty of the existing one.


My code

  1. const itemId = req.body._id;
  2. const userId = req.user._id;
  3. const { qty } = req.body;
  4. try {
  5. const producto = await Product.findById(itemId);
  6. const user = await User.findById(userId).populate({
  7. path: 'carts',
  8. });
  9. const result = user.carts.find((o) => {
  10. console.log(typeof o.product) // returns object
  11. console.log(typeof producto._id); // returns object
  12. return o.product === producto._id
  13. });
  14. console.log(result); // returns undefined
  15. if (result !== undefined) {
  16. const foundCart = await Cart.findById(result._id);
  17. foundCart.qty += qty;
  18. await foundCart.save();
  19. return res.json({ message: 1 });
  20. }
  21. const newCart = new Cart({
  22. qty,
  23. product: producto,
  24. user,
  25. });
  26. const cart = await newCart.save();
  27. user.carts.push(cart);
  28. await user.save();
  29. return res.json({ message: 1 });
  30. } catch (error) {
  31. return console.log(error);
  32. }

答案1

得分: 2

I think the problem is this line

  1. return o.product === producto._id

Can you change it like this and try?

  1. return o.product.toString() === producto._id.toString()
英文:

I think the problem is this line

  1. return o.product === producto._id

Can you change it like this and try?

  1. return o.product.toString() === producto._id.toString()

huangapple
  • 本文由 发表于 2020年1月6日 22:51:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定