'Sequelize ORDER BY ASC numeric string
please help in this matter. So my scenario is that I have driver app (flutter) that fetch ongoing orders that needed to be delivered. I would like to fetch orders according to order
column ASC, I already set that in my backend. Here is the code:
static async getOngoingOrders(driverId, cb) {
try {
const orders = {
instant: [],
scheduled: [],
};
// @scheduled
if (!orders.instant.length) {
const scheduledOrders = await BatchOrder.findAll({
where: {
deliveryDate: {
[Op.gte]: moment().format("YYYY-MM-DD"),
[Op.lt]: moment().add(1, "days").format("YYYY-MM-DD"),
},
},
include: [
{
model: BatchSchedule,
attributes: ["id", "name", "start", "end"],
},
{
model: BatchDriver,
where: {
DriverId: driverId,
status: "Driver on the Way",
},
attributes: ["status", "DriverId"],
include: [
{
model: Order,
where: {
status: {
[Op.not]: ["Not Delivered", "Complained"],
},
},
attributes: ["id", "orderId", "status", "order"],
include: [
{
model: User,
attributes: ["fullName", "phoneNumber", "uid"],
},
{
model: Address,
attributes: [
"name",
"address",
"details",
"notes",
"longitude",
"latitude",
],
},
],
// ******* here is the ASC *******
order: [["order", "ASC"]],
},
],
},
],
attributes: ["id", "deliveryDate"],
});
const formattedScheduledOrder = scheduledOrders.map((batch) => {
const formattedOrder = {
name: batch.BatchSchedule.name,
start: batch.BatchSchedule.start.substr(0, 5),
end: batch.BatchSchedule.end.substr(0, 5),
Orders: [],
};
let delivered = 0;
batch.BatchDrivers[0].Orders.map((order) => {
if (
order.status === "Delivered"
) {
delivered++;
} else {
formattedOrder.Orders.push({
id: order.id,
order: order.order,
orderId: order.orderId,
fullName: order.User.fullName,
phoneNumber: order.User.phoneNumber,
firestoreId: order.User.uid,
Address: {
name: order.Address.name,
address: order.Address.address,
details: order.Address.details,
notes: order.Address.notes,
longitude: order.Address.longitude,
latitude: order.Address.latitude,
},
});
}
});
formattedOrder.delivered = `${delivered}/${batch.BatchDrivers[0].Orders.length}`;
return formattedOrder;
});
orders.scheduled = formattedScheduledOrder;
// ***** tried add this, but got another problem, explanation below *****
// orders.scheduled.map((v) =>
// v.Orders.length > 1
// ? v.Orders.sort((a, b) => a.order - b.order)
// : v.Orders
// );
// console.log(orders.scheduled);
}
cb(null, orders);
} catch (err) {
cb(err, null);
}
}
Here what I got in Insomnia/Postman
As above image, it doesn't effected by order: [["order", "ASC"]]
.
I also have tried to change order: [[Sequelize.literal("Orders.order"), "ASC"]]
.
I also have tried to add map loop as mentioned in above commented code. With that loop I got what I wanted/expected, but I receive another problem. The problem is that in driver app there is a button to change the status (PATCH) of order to delivered/not delivered
, when That button is clicked I will get
// in ongoing orders
{
instant: [],
scheduled: []
}
Please let me know if there are any more info needed, Thanks :)
EDITED
Screenshot of query when fetch on going orders from driver side
Screenshot of fetch orders from user side (INSOMNIA/POSTMAN)
Screenshot of Orders table
EDITED (2)
Screenshot of query after adding separate: true
Screenshot fetch ongoing orders after adding separate: true
I added separate: true
like below:
include: [
{
model: Order,
*** here ***
separate: true,
where: ....
}
]
Solution 1:[1]
Until now I didn't find the solution, So I did sort using js sort built function
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Muhammad Haekal |