什么什么鬼呀
什么什么鬼呀
  • 发布:2025-09-28 22:51
  • 更新:2025-09-28 22:51
  • 阅读:15

【报Bug】云函数保存时间有问题,后端数据库显示是正常的时间戳,但是前端显示Date对象

分类:uniCloud

产品分类: uniCloud/支付宝小程序云

示例代码:
const nowTs = Date.now();  
                        const childIds = [];  
                        for (const item of validReceiverItems) {  
                            const chargesDetail = (item.result && item.result.charge_price_list && item.result  
                                .charge_price_list.charges_detail) || {};  
                            const childDoc = {  
                                batch_order_id: parentId,  
                                status: 0,  
                                status_desc: '待创建',  
                                receiver: item.receiver,  
                                individual_price: toIntOrZero(item.result && item.result.user_pay_money) || toIntOrZero(item.result && item.result.real_pay_money) || toIntOrZero(  
                                    item.result && item.result.total_pay_money) || toIntOrZero(item.result && item  
                                    .result.estimate_pay_money),  
                                price_detail: {  
                                    charges: {  
                                        basic: toIntOrZero(chargesDetail.basic),  
                                        basic_fee: toIntOrZero(chargesDetail.basic_fee),  
                                        over_distance: toIntOrZero(chargesDetail.over_distance),  
                                        over_weight: toIntOrZero(chargesDetail.over_weight),  
                                        cancel_excess_fee: toIntOrZero(chargesDetail.cancel_excess_fee),  
                                        special_time_fee: toIntOrZero(chargesDetail.special_time_fee),  
                                        overflow_fee: toIntOrZero(chargesDetail.overflow_fee),  
                                        gratuity_fee: toIntOrZero(chargesDetail.gratuity_fee),  
                                        vas_fee: toIntOrZero(chargesDetail.vas_fee),  
                                        vas_fee_detail: chargesDetail.vas_fee_detail || {},  
                                        vas_fee_detail_raw: chargesDetail.vas_fee_detail || {}  
                                    },  
                                    free_send_service_fee: toIntOrZero(item.result && item.result  
                                        .free_send_service_fee),  
                                    extra_fee: toIntOrZero(chargesDetail.extra_fee),  
                                    extra_fee_detail: chargesDetail.extra_fee_detail || {},  
                                    total_price: toIntOrZero(item.result && item.result.total_price),  
                                    real_pay_money: toIntOrZero(item.result && item.result.real_pay_money),  
                                    user_pay_money: toIntOrZero(item.result && item.result.user_pay_money) || toIntOrZero(item.result && item.result.real_pay_money),  
                                    thirdparty_charge_price_list: (item.result && item.result.charge_price_list) || {}  
                                },  
                                create_time: nowTs,  
                                update_time: nowTs  
                            };  

                            const addRes = await childCol.add(childDoc);  
                            const childId = addRes.id || addRes._id;  
                            if (childId) childIds.push(childId);  
                        }

操作步骤:
const nowTs = Date.now();  
                        const childIds = [];  
                        for (const item of validReceiverItems) {  
                            const chargesDetail = (item.result && item.result.charge_price_list && item.result  
                                .charge_price_list.charges_detail) || {};  
                            const childDoc = {  
                                batch_order_id: parentId,  
                                status: 0,  
                                status_desc: '待创建',  
                                receiver: item.receiver,  
                                individual_price: toIntOrZero(item.result && item.result.user_pay_money) || toIntOrZero(item.result && item.result.real_pay_money) || toIntOrZero(  
                                    item.result && item.result.total_pay_money) || toIntOrZero(item.result && item  
                                    .result.estimate_pay_money),  
                                price_detail: {  
                                    charges: {  
                                        basic: toIntOrZero(chargesDetail.basic),  
                                        basic_fee: toIntOrZero(chargesDetail.basic_fee),  
                                        over_distance: toIntOrZero(chargesDetail.over_distance),  
                                        over_weight: toIntOrZero(chargesDetail.over_weight),  
                                        cancel_excess_fee: toIntOrZero(chargesDetail.cancel_excess_fee),  
                                        special_time_fee: toIntOrZero(chargesDetail.special_time_fee),  
                                        overflow_fee: toIntOrZero(chargesDetail.overflow_fee),  
                                        gratuity_fee: toIntOrZero(chargesDetail.gratuity_fee),  
                                        vas_fee: toIntOrZero(chargesDetail.vas_fee),  
                                        vas_fee_detail: chargesDetail.vas_fee_detail || {},  
                                        vas_fee_detail_raw: chargesDetail.vas_fee_detail || {}  
                                    },  
                                    free_send_service_fee: toIntOrZero(item.result && item.result  
                                        .free_send_service_fee),  
                                    extra_fee: toIntOrZero(chargesDetail.extra_fee),  
                                    extra_fee_detail: chargesDetail.extra_fee_detail || {},  
                                    total_price: toIntOrZero(item.result && item.result.total_price),  
                                    real_pay_money: toIntOrZero(item.result && item.result.real_pay_money),  
                                    user_pay_money: toIntOrZero(item.result && item.result.user_pay_money) || toIntOrZero(item.result && item.result.real_pay_money),  
                                    thirdparty_charge_price_list: (item.result && item.result.charge_price_list) || {}  
                                },  
                                create_time: nowTs,  
                                update_time: nowTs  
                            };  

                            const addRes = await childCol.add(childDoc);  
                            const childId = addRes.id || addRes._id;  
                            if (childId) childIds.push(childId);  
                        }

重点是const nowTs = Date.now(); create_time: nowTs,

预期结果:

前端获取数据格式应该是 "create_date":1759042641734, 

实际结果:

"create_date":new Date("Sun Sep 28 2025 15:47:04 GMT+0800 (香港标准时间)"),

bug描述:

后端数据显示是正常的时间戳形式,但是前端获取还是new date,目前因为云函数创建数据不会自动触发时间create_date的生成,所以我就使用Date.now()来进行生成,但是云函数保存数据以后,前端获取的数据格式是 "create_date":new Date("Sun Sep 28 2025 17:04:56 GMT+0800 (香港标准时间)")

数据库 "defaultValue": {
"$env": "now"
}触发的时间保存,前端显示是正常的时间戳

你可以看下面第二张图,第一个是用云函数 Data.now() 插入数据库的,第二个是用数据库触发的,一个是时间戳一个直接是时间了,后端数据库中,显示是正常的时间戳

2025-09-28 22:51 负责人:无 分享
已邀请:

要回复问题请先登录注册