// 云函数入口文件 const cloud = require('wx-server-sdk') cloud.init() // 云函数入口函数 exports.main = async (event, context) => { const wxContext = cloud.getWXContext() var n = Date.now() + 8 * 3600000; var date = new Date(n); var Y = date.getFullYear(); var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1); var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours(); var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(); var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(); var Y1 = Y; Y = Y * 100; const charts = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; var res = ''; for (var i = 0; i < 5; i++) { var id = Math.ceil(Math.random() * 9); res += charts[id]; } var id = res; return { time: (Y1 + '-' + M + '-' + D + " " + h + ':' + m + ':' + s), orderid: (Y + M + D + h + m + s + id) } } 在问题中那个图里,在后面加了个console.log发现赋值是成功的,但作用不到全局里...
// 云函数入口函数 exports.main = async (event, context) => { const wxContext = cloud.getWXContext() var n = Date.now() + 8 * 3600000; var date = new Date(n); var Y = date.getFullYear(); var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1); var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours(); var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(); var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(); var Y1 = Y; Y = Y * 100; const charts = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; var res = ''; for (var i = 0; i < 5; i++) { var id = Math.ceil(Math.random() * 9); res += charts[id]; } var id = res; return await { time: (Y1 + '-' + M + '-' + D + " " + h + ':' + m + ':' + s), orderid: (Y + M + D + h + m + s + id) } } 我这个替换了,你试一下,记得上传云函数
没那没复杂吧,也没必要使用云函数
getTimeAndId: function () {
let ranNum = (len) => {
var m = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], res = []
while (len) {
var b = parseInt(Math.random() * len) - 1,
res.push(m.splice(b, 1).shift())
len -= 1
}
return res
}, date = new Date(new Date().getTime() + 8 * 3600 * 1000), data = {}
data.time = date.toJSON().replace(/T/g, ' ').replace(/\.\d+Z$/g, '').trim()
data.orderId = data.time.replace(/[-: ]/g, '').trim() + ranNum(5).join('')
console.log(data)
}
//===============================
上面的 ranNum 方法
随机5个数字 ranNum(5) => 如:[2,4,5,8,7]
问题结论:这问题产生的错误不在于赋值,而在于运行顺序。代码里先获取时间,再上传数据,但在控制台里却显示先上传,再返回时间。这也是为什么我运行第二次时发现存在数值的原因。之后更改两个函数的位置即可。(下图为未改前的情况)
你的云函数是如何写的,复制代码我看看
const cloud = require('wx-server-sdk')
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
var n = Date.now() + 8 * 3600000;
var date = new Date(n);
var Y = date.getFullYear();
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
var Y1 = Y;
Y = Y * 100;
const charts = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
var res = '';
for (var i = 0; i < 5; i++) {
var id = Math.ceil(Math.random() * 9);
res += charts[id];
}
var id = res;
return {
time: (Y1 + '-' + M + '-' + D + " " + h + ':' + m + ':' + s),
orderid: (Y + M + D + h + m + s + id)
}
}
在问题中那个图里,在后面加了个console.log发现赋值是成功的,但作用不到全局里...
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
var n = Date.now() + 8 * 3600000;
var date = new Date(n);
var Y = date.getFullYear();
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
var Y1 = Y;
Y = Y * 100;
const charts = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
var res = '';
for (var i = 0; i < 5; i++) {
var id = Math.ceil(Math.random() * 9);
res += charts[id];
}
var id = res;
return await {
time: (Y1 + '-' + M + '-' + D + " " + h + ':' + m + ':' + s),
orderid: (Y + M + D + h + m + s + id)
}
}
我这个替换了,你试一下,记得上传云函数