guide数据删除

# 概述

有几种不同的方法可以从服务器删除数据

# 删除文档

如您所见,上述所有请求仅删除整个文档的一部分。根据您的需要,您可能希望删除整个文档,包括所有评论、建议等。为此,您可以使用 文档 REST API

请记住,此操作不可逆转。所有文档数据将从数据库中永久删除。

# 示例

以下示例已在 Node.js 中准备。

  1. 运行以下命令
mkdir cs-document-delete-example && cd cs-document-delete-example && npm init -y && npm i axios && touch delete.cjs
  1. 打开 cs-document-delete-example/delete.cjs 并粘贴以下代码片段
const crypto = require( 'crypto' );
const axios = require( 'axios' );

// Update with your credentials and application endpoint
const environmentId = 'txQ9sTfqmXUyWU5LmDbr';
const apiSecret = '4zZBCQoPfRZ7Rr7TEnGAuRsGgbfF58Eg0PA8xcLD2kvPhjGjy4VGgB8k0hXn';
const applicationEndpoint = 'https://33333.cke-cs.com';

// Set document id
const documentId = 'my_document_id';

const deleteApiEndpoint = `${ applicationEndpoint }/api/v5/${ environmentId }/documents/${ documentId }`;

( async () => {
    await deleteDocument();

    async function deleteDocument( ) {
        try {
            const config = getConfig( 'DELETE', deleteApiEndpoint );
            const response = await axios.delete( deleteApiEndpoint, config );

            console.log( response.status );
        } catch ( error ) {
            console.log( error.message );
            console.log( error.response.data );
        }
    }
} )();

function getConfig( method, url ) {
    const CSTimestamp = Date.now();

    return {
        headers: {
            'X-CS-Timestamp': CSTimestamp,
            'X-CS-Signature': generateSignature( apiSecret, method, url, CSTimestamp )
        }
    };
}

function generateSignature( secret, method, uri, timestamp, body ) {
    const url = new URL( uri );
    const path = url.pathname + url.search;
    const hmac = crypto.createHmac( 'SHA256', secret );

    hmac.update( `${ method.toUpperCase() }${ path }${ timestamp }` );

    if ( body ) {
        hmac.update( Buffer.from( JSON.stringify( body ) ) );
    }

    return hmac.digest( 'hex' );
}
  1. 使用现有文档的 ID 更新您的凭据和代码片段中的 documentId 值。

  2. 执行 node delete.cjs 命令。

删除请求成功响应状态代码 204 后,将从服务器中删除包含所有资源的文档。

# 故障排除

  • 我们建议您启用 洞察面板,您将在其中找到整个过程的详细日志,包括任何失败请求的确切原因。