简体   繁体   English

尝试在 Angular UI 中显示时,提取的已排序 API 数据(NodeJs &Mongoose)未按排序顺序显示

[英]Fetched sorted API data(NodeJs &Mongoose) not getting displayed in sorted order when try display in Angular UI

I have tried to get sorted in backend & tested via postman and I am getting sorted order.我试图在后端进行排序并通过 postman 进行测试,我得到了排序。

const locationInfo = await locationDetails.find(query).sort({sectionName:1});
res.json(locationInfo);

[
    {    //some other keys &values
        "sectionName": "Closet",
    },
    {
        "sectionName": "Dining",

    },
    {
        "sectionName": "Kitchen",

    },
    {

        "sectionName": "Other",
    },
    {

        "sectionName": "Refrigerator",
    }
]

After REST call storing result to, REST调用存储结果之后,

this.result=data;

but when I try to display the same resultant data on UI, Its not getting displayed in sorted order as well as checked in console also resultant data order got changed.但是当我尝试在 UI 上显示相同的结果数据时,它没有按排序顺序显示,也没有在控制台中检查,结果数据顺序也发生了变化。

Console Data控制台数据

[{
sectionName: "Refrigerator",
},
{
sectionName: "Kitchen",
},
{
sectionName: "Dining",
},
{
sectionName: "Closet",
},
{
sectionName: "Other",
}]

Note: Tried to sort from.ts file also but it is not working.注意:也尝试从 .ts 文件排序,但它不起作用。

this.result.sort(function(a,b){a.sectionName-b.sectionName});

If any help would be appreciated.如果有任何帮助将不胜感激。 Thanks!谢谢!

SectioName is not a valid criterion for MongoDB to sort the return result. SectioName不是 MongoDB 对返回结果进行排序的有效标准。 In this case, MongoDB does not know how to sort it.在这种情况下,MongoDB 不知道如何排序。

Here is an example directly from the MongoDB documentation about cursor.sort() :以下是 MongoDB 文档中有关cursor.sort()的示例:

db.restaurants.insertMany( [
   { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"},
   { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"},
   { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"},
   { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"},
   { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"},
] );

# The following command uses the sort() method to sort on the borough field:
db.restaurants.find().sort( { "borough": 1 } )

Documents are returned in alphabetical order by borough, but the order of those documents with duplicate values for borough might not be the same across multiple executions.文档按 borough 按字母顺序返回,但那些具有 borough 重复值的文档的顺序在多次执行中可能不同。

.sort works best with numerical values. .sort最适用于数值。 If you are in control of the backend and are able to change how data is stored in the database.如果您控制后端并且能够更改数据在数据库中的存储方式。 I suggest you create a field for the creation date or just an index to indicate the order of the items.我建议您为创建日期创建一个字段,或者只是一个索引来指示项目的顺序。

Let's say your document looks something like this:假设您的文档如下所示:

# Doc 1
{
sectionName: "Refrigerator",
order:1
}
# Doc 2
{
sectionName: "Refrigerator",
order:2
}

Then you can do然后你可以做

const locationInfo = await locationDetails.find(query).sort({order:1});

which will return you the documents sorted using the order field, and the order will be consistent.这将返回您使用 order 字段排序的文档,并且顺序将是一致的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM