-
Notifications
You must be signed in to change notification settings - Fork 25
Open
Description
问题描述
使用云日志服务 SDK sls20201230
的 putLogs
方法报错 TypeError: Cannot read properties of null (reading 'length')
,使用 API 调试台 生成的 SDK 示例代码同样报错。但使用 getLogStore
可正常获取 Store 信息,说明 AK 和 project store 配置正常。
推测可能是 @alicloud/gateway-sls.Client.modifyRequest
方法中初始化请求体数据 bodyBytes
的问题,报错详细信息:
D:\Codes\Yike\ykerp-nest-service\node_modules\.pnpm\@[email protected]\node_modules\@alicloud\gateway-sls-util\src\client.ts:74
return src.length;
^
TypeError: Cannot read properties of null (reading 'length')
at Function.bytesLength (D:\Codes\Yike\ykerp-nest-service\node_modules\.pnpm\@[email protected]\node_modules\@alicloud\gateway-sls-util\src\client.ts:74:16)
at Client.modifyRequest (D:\Codes\Yike\ykerp-nest-service\node_modules\.pnpm\@[email protected]\node_modules\@alicloud\gateway-sls\src\client.ts:98:39)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async Client.execute (D:\Codes\Yike\ykerp-nest-service\node_modules\.pnpm\@[email protected]\node_modules\@alicloud\openapi-core\src\client.ts:1287:9)
[email protected]\node_modules\@alicloud\sls20201230\src\client.ts:7792:12)
at async putLogs (D:\Codes\Yike\ykerp-nest-service\src\aliyun\aliyun-log.service.ts:61:20)
Node.js v22.14.0
运行环境
- Windows 11 专业版 24H2 26100.4351
- Node.js - 22.14.0
- npm - 11.4.2
- @alicloud/sls20201230 - 5.7.2
- @alicloud/credentials - 2.4.3
- @alicloud/openapi-client - 0.4.14
- @alicloud/tea-typescript - 1.8.0
- @alicloud/tea-util - 1.4.10
示例代码与运行输出
import Credential, * as $Credential from '@alicloud/credentials'
import * as $OpenApi from '@alicloud/openapi-client'
import Sls, * as $Sls from '@alicloud/sls20201230'
/**
* 创建阿里云日志服务客户端
* @description 从环境变量获取配置创建阿里云日志服务客户端实例,需要的环境变量包括:
* - `ALIBABA_CLOUD_ACCESS_KEY_ID` - 阿里云访问密钥 ID
* - `ALIBABA_CLOUD_ACCESS_KEY_SECRET` - 阿里云访问密钥 Secret
* - `ALIBABA_CLOUD_SLS_ENDPOINT` - 阿里云日志服务的接入点
* @returns 阿里云日志服务客户端
*/
function createSlSClient(): Sls {
/**
* 阿里云凭据
* @description 配置方式请参见:https://help.aliyun.com/document_detail/378664.html
*/
const credential = new Credential(
new $Credential.Config({
type: 'access_key',
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
})
)
/** 阿里云 OpenApi 配置 */
const config = new $OpenApi.Config({
credential,
endpoint: process.env.ALIBABA_CLOUD_SLS_ENDPOINT
})
// 阿里云日志服务
return new Sls(config)
}
const slsClient = createSlSClient()
async function putLogs(
project: string,
store: string,
contents: Pick<$Sls.LogContent, 'key' | 'value'>[]
): Promise<$Sls.PutLogsResponse> {
// 查询 LogStore 信息
const logStore = await slsClient.getLogStore(project, store)
console.log('LogStore:', logStore)
// 创建日志项
const logItem = new $Sls.LogItem({})
logItem.time = Math.round(Date.now() / 1000) // 时间戳,单位为秒
logItem.contents = contents.map((content) => new $Sls.LogContent(content))
console.log('LogItem:', logItem)
// 创建日志组
const logGroup = new $Sls.LogGroup({})
logGroup.logItems = [logItem]
console.log('LogGroup:', logGroup)
// 创建并发起 PutLogs 请求
const request = new $Sls.PutLogsRequest({ body: logGroup })
console.log('PutLogsRequest:', request)
const response = await slsClient.putLogs(project, store, request)
console.log('PutLogs response:', response)
return response
}
putLogs('ykerp-test', 'backend', [
{
key: 'message',
value: 'Hello, world!'
}
])
LogStore: GetLogStoreResponse {
headers: {
server: 'AliyunSLS',
'content-type': 'application/json',
'content-length': '320',
connection: 'keep-alive',
'access-control-allow-origin': '*',
date: 'Fri, 27 Jun 2025 08:38:10 GMT',
'x-log-time': '1751013490',
'x-log-requestid': '685E587242DD8DBFC94D4D21'
},
statusCode: 200,
body: Logstore {
appendMeta: true,
autoSplit: true,
createTime: 1734682369,
enableTracking: false,
hotTtl: 30,
lastModifyTime: 1750628422,
logstoreName: 'backend',
maxSplitShard: 64,
mode: 'standard',
productType: '',
shardCount: 2,
telemetryType: '',
ttl: 30
}
}
LogItem: LogItem {
time: 1751013489,
contents: [ LogContent { key: 'message', value: 'Hello, world!' } ]
}
LogGroup: LogGroup {
logItems: [ LogItem { time: 1751013489, contents: [Array] } ]
}
PutLogsRequest: PutLogsRequest { body: LogGroup { logItems: [ [LogItem] ] } }
D:\Codes\Yike\ykerp-nest-service\node_modules\.pnpm\@[email protected]\node_modules\@alicloud\gateway-sls-util\src\client.ts:74
return src.length;
^
TypeError: Cannot read properties of null (reading 'length')
at Function.bytesLength (D:\Codes\Yike\ykerp-nest-service\node_modules\.pnpm\@[email protected]\node_modules\@alicloud\gateway-sls-util\src\client.ts:74:16)
at Client.modifyRequest (D:\Codes\Yike\ykerp-nest-service\node_modules\.pnpm\@[email protected]\node_modules\@alicloud\gateway-sls\src\client.ts:98:39)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async Client.execute (D:\Codes\Yike\ykerp-nest-service\node_modules\.pnpm\@[email protected]\node_modules\@alicloud\openapi-core\src\client.ts:1287:9)
[email protected]\node_modules\@alicloud\sls20201230\src\client.ts:7792:12)
at async putLogs (D:\Codes\Yike\ykerp-nest-service\src\aliyun\aliyun-log.service.ts:61:20)
Node.js v22.14.0
Metadata
Metadata
Assignees
Labels
No labels