云函数保存时间有问题 uni-app 后端数据库显示是正常的时间戳但是前端显示Date对象

云函数保存时间有问题 uni-app 后端数据库显示是正常的时间戳但是前端显示Date对象

示例代码:

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 17:04:56 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() 插入数据库的,第二个是用数据库触发的,一个是时间戳一个直接是时间了,后端数据库中,显示是正常的时间戳

示例图片

示例图片


更多关于云函数保存时间有问题 uni-app 后端数据库显示是正常的时间戳但是前端显示Date对象的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

查询的时候返回的数据是啥呢?

更多关于云函数保存时间有问题 uni-app 后端数据库显示是正常的时间戳但是前端显示Date对象的实战教程也可以访问 https://www.itying.com/category-93-b0.html


“create_date”:new Date(“Sun Sep 28 2025 15:47:04 GMT+0800 (香港标准时间)”),
用云函数 Date.now() 保存的数据,返回数据就是上面这个
但是前端直接请求,然后defaultvalue保存的数据,前端返回 “create_date”:1759042641734

},
“item_info”: {
“type”: “日用品,1kg”,
“weight”: 1000,
“type_id”: 5
},
“sf_bill_id”: “SF6505381636380”,
“create_date”:new Date(“Sun Sep 28 2025 17:04:56 GMT+0800 (香港标准时间)”),
“pickup_code”: 2736,
“sf_order_id”: “JS4151116257225”,
“status_desc”: “订单创建”,
“total_price”: 1300,
“update_date”: 1759050298942,
“order_status”: 1,
“out_trade_no”: “BATCH_1759050292005-balance”,
“price_detail”: { 上面返回的是new Date(“Sun Sep 28 2025 17:04:56 GMT+0800 (香港标准时间)”) 这个数据

问题出在云函数返回数据时的序列化处理上。当云函数返回包含时间戳字段的数据时,uniCloud SDK会自动将时间戳转换为Date对象。

解决方案是在云函数返回数据前,手动将时间戳字段转换为字符串或数字类型:

// 在云函数返回数据前处理
const result = {
    create_time: nowTs.toString(), // 或者直接返回 nowTs
    // 其他字段...
}
return result

或者在前端获取数据后进行处理:

// 前端处理
const data = await uniCloud.callFunction({...})
data.result.create_time = new Date(data.result.create_time).getTime()
回到顶部