有一半多的概率会报这个错,其他一般概率正常。
代码:
// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程:https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const GLOBAL_CONFIG_KEY = 'global-config' // Redis中存储全局配置的key
module.exports = {
_before: function () { // 通用预处理器
},
/**
* 获取全局配置
* @returns {object} 返回全局配置
*/
async getGlobalConfig() {
try {
const redis = uniCloud.redis()
// 先从Redis获取
const redisConfig = await redis.get(GLOBAL_CONFIG_KEY)
if (redisConfig) {
console.log('从Redis获取到全局配置')
return {
code: 0,
msg: '获取成功(Redis)',
data: JSON.parse(redisConfig)
}
}
// Redis没有,从数据库获取
console.log('Redis中无配置,从数据库获取')
const db = uniCloud.database()
const configRecord = await db.collection('config').where({
key: GLOBAL_CONFIG_KEY
}).get()
const resultData = configRecord.data || (configRecord.result && configRecord.result.data) || []
if (resultData.length > 0) {
const config = resultData[0].value
// 保存到Redis
await redis.set(GLOBAL_CONFIG_KEY, JSON.stringify(config))
console.log('获取到全局配置并保存到Redis:', config)
return {
code: 0,
msg: '获取成功(DB)',
data: config
}
} else {
return {
code: 0,
msg: '配置不存在',
data: null
}
}
} catch (error) {
console.error('获取全局配置失败:', error)
return {
code: -1,
msg: '获取失败',
error: error.message
}
}
},
/**
* 更新全局配置
* @param {object} config 配置对象
* @returns {object} 返回更新结果
*/
async updateGlobalConfig(config) {
if (!config) {
return {
code: -1,
msg: '配置参数不能为空'
}
}
try {
const db = uniCloud.database()
const redis = uniCloud.redis()
const configRecord = await db.collection('config').where({
key: GLOBAL_CONFIG_KEY
}).get()
const resultData = configRecord.data || (configRecord.result && configRecord.result.data) || []
let result
if (resultData.length > 0) {
result = await db.collection('config').where({
key: GLOBAL_CONFIG_KEY
}).update({
value: config
})
} else {
result = await db.collection('config').add({
key: GLOBAL_CONFIG_KEY,
value: config
})
}
// 更新Redis缓存
await redis.set(GLOBAL_CONFIG_KEY, JSON.stringify(config))
console.log('更新全局配置成功(DB和Redis):', result)
return {
code: 0,
msg: '更新成功',
data: result
}
} catch (error) {
console.error('更新全局配置失败:', error)
return {
code: -1,
msg: '更新失败',
error: error.message
}
}
}
}
日志:
16:51:27.916 [本地运行]删除所有Redis键失败: uniCloud-tcb/cloudfunctions/aspack-redis/index.obj.js:216:11
16:51:27.916 [本地运行]TypeError [ERR_STREAM_NULLVALUES]: May not write null values to stream
16:51:27.916 [本地运行] at new NodeError (node:internal/errors:405:5)
16:51:27.916 [本地运行] at write (node:_http_outgoing:873:11)
16:51:27.916 [本地运行] at ClientRequest.write (node:_http_outgoing:834:15)
16:51:27.916 [本地运行] at new Promise (<anonymous>)
16:51:27.916 [本地运行] at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
16:51:27.916 [本地运行] at async D:\software\HBuilderX.4.15.2024050802\HBuilderX\plugins\unicloud\share\ulib.js:1:177501
16:51:27.931 [本地运行][云对象:aspack-redis],调用方法:[delAllKeys],执行结果: {"code":-1,"msg":"删除失败","error":"May not write null values to stream"}
3 个回复
DCloud_uniCloud_CRL
完整代码发一下
aspack001 (作者) - 老程序猿
我之前发的是云函数的代码,下面是调用方的代码,在const aspackConfig = uniCloud.importObject('aspack-config')的时候会偶尔抛我说的错误,难道是我调用方法有问题?:
aspack001 (作者) - 老程序猿
我似乎找到原因了,我的云函数内存设置的128M,改成256M就好了。