我对javascript并不陌生,但这是我第一次使用node.js。我正在尝试创建一个基本的查询来提供给mongoose,然后在稍后添加条件。我使用deepmerge将这些散列合并在一起,但是objectid被破坏了。
原始哈希:
query = {
$and: [
{"unsubscribe.status": { "$ne": "unsubscribed" }},
{"isDeleted.status": { "$ne": true }},
{'advisorCommunities': { $in: communities }},
{"memberType": 'Advisor'}
]
}控制台目录:
'$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' }
]
}要合并的哈希:
engageQuery = {
'$and': [
{
'$or': [
{ nextEngagementDate: { '$exists': false } }
]
}
}
var deepmerge = require('deepmerge');
query = deepmerge(engageQuery, query);advisorCommunities已更改:
{
'$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的错误:
(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.我花了一个小时尝试一些奇怪的东西来创建自定义函数,等等,但失败了。请帮帮忙。谢谢!
发布于 2021-06-11 03:18:18
我回答了我自己的问题,但我不确定这是正确的答案。
query = deepmerge(engageQuery, query, {clone: false});https://www.npmjs.com/package/deepmerge
将clone: false添加到选项修复了这个问题。但是,根据npm文档,该选项已被弃用。有人知道做这件事的正确方法吗?
发布于 2022-01-20 12:44:18
在is-plain-object模块中使用isMergeableObject选项可能会更好。官方例子:
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
})https://stackoverflow.com/questions/67927041
复制相似问题