Mongoose数组不会排序

时间:2018-02-11 09:56:53

标签: arrays node.js mongodb sorting

mongodb文档包含一个数组。当使用mongoose和graphql检索到前端时,在数组上使用.sort()会引发错误。

  

无法分配给对象'[object Array]'

的只读属性'1'

但是,在完全相同的数组上使用.sort()(但这次使用硬编码)可以正常工作。

这里发生了什么?!

{ "presets": [
          ["@babel/preset-env", { 
                //"debug": true, 
                "targets": {
                    "browsers": [
                        "> 1%",
                        "last 2 versions",
                        "chrome 41",
                        "ie >= 9",
                        "safari >= 7"
                    ]
                },
            "useBuiltIns": 'usage'
          }]
]}

[编辑:这是mongo文件]

const c = [ { slug: 'first-element',
    position: 1,
    alphaPos: 'B',
    type: 'heading',
    data: 'first-el B' },
{ slug: 'second-element',
    position: 2,
    alphaPos: 'C',
    type: 'textarea',
    data: 'second-el C' },
{ slug: 'third-element',
    position: 3,
    alphaPos: 'A',
    type: 'textarea',
    data: 'third-el A' },
{ slug: 'third-b-element',
    position: 3,
    alphaPos: 'D',
    type: 'textarea',
    data: 'third-el-22222 D' } ]

    // 'content' is the same as 'c' but it comes from mongoose, 
    // it throws an error
    console.log('content ==', content.sort())

    // 'c' is hard coded, no error is thrown
    console.log('content ==', c.sort())

    // console.log(c) and console.log(content) output exactly the same  array

这是架构

    {
        "_id" : ObjectId("5a801422f1c5437466c5d285"),
        "createdAt" : ISODate("2018-02-11T10:00:02.800Z"),
        "id" : 1,
        "slug" : "interesting-story",
        "creatorId" : 3,
        "title" : "An interesting story",
        "image" : "/images/image1.jpg",
        "description" : "description",
        "content" : [ 
            {
                "slug" : "first-element",
                "position" : 1,
                "alphaPos" : "B",
                "type" : "heading",
                "data" : "first-el B"
            }, 
            {
                "slug" : "second-element",
                "position" : 2,
                "alphaPos" : "C",
                "type" : "textarea",
                "data" : "second-el C"
            }, 
            {
                "slug" : "third-element",
                "position" : 3,
                "alphaPos" : "A",
                "type" : "textarea",
                "data" : "third-el A"
            }, 
            {
                "slug" : "third-b-element",
                "position" : 3,
                "alphaPos" : "D",
                "type" : "textarea",
                "data" : "third-el-22222 D"
            }
        ],
        "__v" : 0
    }

2 个答案:

答案 0 :(得分:0)

因为这些是两种不同的排序,取决于你所谓的排序。如果你在数组上调用它(“硬编码”)它使用默认排序函数https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort,如果你不能在mongodocument对象类型上调用它而不编写sort函数。

要使用猫鼬排序,您必须在构建查询时应用它

collection.find().sort().then(...)

答案 1 :(得分:0)

如果有人遇到相同的错误,我仍然无法解释它,但数组被认为是索引是键的对象......

如果有人对此行为有解释,我会感兴趣(与graphql相关?对于mongoose?可能是对graphql-type-json包?)。

所以要对它进行排序,这就是我正在做的事情(输出一个数组)。

Object.keys(content).sort(a=>1).reduce((_sortedObj, key) => {
    _sortedObj.push(content[key])
    return _sortedObj
    }, [])