Chuyển đến nội dung chính

Lấy thuộc tính theo marker

Trong ví dụ này, chúng tôi sẽ trình bày cách lấy sơ đồ thuộc tính cho một bộ thuộc tính cụ thể bằng marker của nó thông qua API OneEntry.

✅ Mục đích của kịch bản:

  • Kết nối với API OneEntry
  • Lấy sơ đồ thuộc tính cho một bộ thuộc tính đã đặt tên
  • Truy cập siêu dữ liệu thuộc tính: marker, type, label, validators, và listTitles

✅ Những gì bạn cần:

  • Một PROJECT_URL và APP_TOKEN hợp lệ để xác thực với API OneEntry.
  • Một bộ thuộc tính được cấu hình trong OneEntry với một marker đã biết (ví dụ: "product")

📌 Quan trọng:

  • Những ví dụ này không bao gồm xử lý lỗi.
  • Bạn có thể quản lý lỗi bằng cách sử dụng khối try-catch hoặc bằng cách sử dụng cấu trúc như await Promise.catch((error) => error).
  • Trả về SCHEMA của các thuộc tính — KHÔNG phải là giá trị thực tế của các thực thể.
  • value luôn là trong schema — để lấy giá trị thực, hãy sử dụng Products.getProducts(), Pages.getPageByUrl(), v.v.
  • Sử dụng listTitles để xây dựng các tùy chọn lọc cho các thuộc tính loại list và radioButton.

📚 Xem trong tài liệu:

📦 Tham khảo SDK:

Thử nghiệm trực tiếp

Chạy phương thức này một cách tương tác trong JS SDK sandbox — kết nối URL Dự án và Mã thông báo Ứng dụng của bạn khi lần đầu truy cập, sau đó mở:

  • Lấy thuộc tính theo marker — Trong ví dụ này, chúng tôi sẽ trình bày cách lấy sơ đồ thuộc tính cho một bộ thuộc tính cụ thể bằng marker của nó thông qua API OneEntry.

Kịch bản

1. Nhập defineOneEntry từ SDK và định nghĩa PROJECT_URL và APP_TOKEN

Ví dụ:

import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';

2. Tạo một khách hàng API với defineOneEntry()

Ví dụ:

const { AttributesSets } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

3. Lấy sơ đồ thuộc tính theo marker với AttributesSets.getAttributesByMarker()

Ví dụ:

const attributes = await AttributesSets.getAttributesByMarker(
'product',
'en_US',
);
console.log('Attributes: ', attributes);
Kết quả:
[
{
"type": "real",
"value": {},
"marker": "price",
"position": 1,
"listTitles": [],
"validators": {
"requiredValidator": {
"strict": true
}
},
"initialValue": null,
"localizeInfos": {
"title": "Price"
},
"additionalFields": {}
},
{
"type": "string",
"value": {},
"marker": "currency",
"position": 2,
"listTitles": [],
"validators": {
"defaultValueValidator": {
"fieldDefaultValue": "USD",
"fieldDefaultValue2": ""
}
},
"initialValue": null,
"localizeInfos": {
"title": "Currency"
},
"additionalFields": {}
},
{
"type": "image",
"value": {},
"marker": "image",
"position": 3,
"listTitles": [],
"validators": {},
"initialValue": null,
"localizeInfos": {
"title": "Image"
},
"additionalFields": {}
}
]

Ví dụ cuối cùng

// 1. Import defineOneEntry from SDK and define PROJECT_URL and APP_TOKEN
import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';

// 2. Creating an API client with [defineOneEntry()](/docs/index/#Installation)
const { AttributesSets } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

// 3. Get attribute schema by marker with [AttributesSets.getAttributesByMarker()](/docs/attribute-sets/getAttributeSetByMarker)
const attributes = await AttributesSets.getAttributesByMarker(
'product',
'en_US',
);
console.log('Attributes: ', attributes);