首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript深度合并中断objectid

Javascript深度合并中断objectid
EN

Stack Overflow用户
提问于 2021-06-11 02:57:44
回答 2查看 25关注 0票数 0

我对javascript并不陌生,但这是我第一次使用node.js。我正在尝试创建一个基本的查询来提供给mongoose,然后在稍后添加条件。我使用deepmerge将这些散列合并在一起,但是objectid被破坏了。

原始哈希:

代码语言:javascript
复制
query = {
        $and: [
            {"unsubscribe.status": { "$ne": "unsubscribed" }},
            {"isDeleted.status": { "$ne": true }},
            {'advisorCommunities': { $in: communities }},
            {"memberType": 'Advisor'}
        ]
    }

控制台目录:

代码语言:javascript
复制
  '$and': [
    { 'unsubscribe.status': { '$ne': 'unsubscribed' } },
    { 'isDeleted.status': { '$ne': true } },
    {
      advisorCommunities: {
        '$in': [
          ObjectID {
            _bsontype: 'ObjectID',
            id: Buffer(12) [Uint8Array] [
               93,   2, 154,  46,  92,
              186,  99,   7, 226, 238,
              107, 170
            ]
          }
        ]
      }
    },
    { memberType: 'Advisor' }
  ]
}

要合并的哈希:

代码语言:javascript
复制
engageQuery = {
  '$and': [
    {
      '$or': [
        { nextEngagementDate: { '$exists': false } }
      ]
    }
}

var deepmerge = require('deepmerge');
query = deepmerge(engageQuery, query);

advisorCommunities已更改:

代码语言:javascript
复制
{
  '$and': [
    {
      '$or': [
        { nextEngagementDate: { '$gte': 2021-06-10T15:17:54.627Z } },
        { nextEngagementDate: { '$exists': false } }
      ]
    },
    { 'unsubscribe.status': { '$ne': 'unsubscribed' } },
    { 'isDeleted.status': { '$ne': true } },
    {
      advisorCommunities: {
        '$in': [
          {
            _bsontype: 'ObjectID',
            id: {
              '0': 93,
              '1': 2,
              '2': 154,
              '3': 46,
              '4': 92,
              '5': 186,
              '6': 99,
              '7': 7,
              '8': 226,
              '9': 238,
              '10': 107,
              '11': 170
            }
          }
        ]
      }
    },
    { memberType: 'Advisor' }
  ]
}

这是来自mongoose的错误:

代码语言:javascript
复制
(node:34291) UnhandledPromiseRejectionWarning: CastError: Cast to string failed for value "{
  _bsontype: 'ObjectID',
  id: {
    '0': 93,
    '1': 2,
    '2': 154,
    '3': 46,
    '4': 92,
    '5': 186,
    '6': 99,
    '7': 7,
    '8': 226,
    '9': 238,
    '10': 107,
    '11': 170
  }
}" at path "advisorCommunities" for model "Member"
    at model.Query.exec (/protopia/api/node_modules/mongoose/lib/query.js:4360:21)
    at model.Query.Query.then (/protopia/api/node_modules/mongoose/lib/query.js:4454:15)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:34291) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:34291) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我花了一个小时尝试一些奇怪的东西来创建自定义函数,等等,但失败了。请帮帮忙。谢谢!

EN

回答 2

Stack Overflow用户

发布于 2021-06-11 03:18:18

我回答了我自己的问题,但我不确定这是正确的答案。

代码语言:javascript
复制
query = deepmerge(engageQuery, query, {clone: false});

https://www.npmjs.com/package/deepmerge

将clone: false添加到选项修复了这个问题。但是,根据npm文档,该选项已被弃用。有人知道做这件事的正确方法吗?

票数 0
EN

Stack Overflow用户

发布于 2022-01-20 12:44:18

is-plain-object模块中使用isMergeableObject选项可能会更好。官方例子:

代码语言:javascript
复制
const isPlainObject = require('is-plain-object')
 
function SuperSpecial() {
    this.special = 'oh yeah man totally'
}
 
const instantiatedSpecialObject = new SuperSpecial()
 
const target = {
    someProperty: {
        cool: 'oh for sure'
    }
}
 
const source = {
    someProperty: instantiatedSpecialObject
}
 
const defaultOutput = merge(target, source)
 
defaultOutput.someProperty.cool // => 'oh for sure'
defaultOutput.someProperty.special // => 'oh yeah man totally'
defaultOutput.someProperty instanceof SuperSpecial // => false
 
const customMergeOutput = merge(target, source, {
    isMergeableObject: isPlainObject
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67927041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档