Mã
js
// verify-destination.js - Node 22, no dependencies
// Usage: node verify-destination.js <id> '<expected JSON>'
const DEST_URL = "https://example.com/api/orders"; // read endpoint of the destination
const API_KEY = process.env.DEST_API_KEY || "YOUR_API_KEY";
// Read the record from the destination. Returns null when it does not exist.
async function readBack(id) {
const res = await fetch(`${DEST_URL}/${encodeURIComponent(id)}`, {
headers: { Authorization: `Bearer ${API_KEY}` },
});
if (res.status === 404) return null;
if (!res.ok) throw new Error(`Read-back failed: HTTP ${res.status}`);
return res.json();
}
// List every expected field that is missing or different at the destination.
function diff(expected, actual) {
const mismatches = [];
for (const [field, want] of Object.entries(expected)) {
const got = actual[field];
if (got === undefined || String(got) !== String(want)) {
mismatches.push({ field, expected: want, actual: got ?? null });
}
}
return mismatches;
}
// Check the destination, retry a few times, never modify anything.
async function verifyAtDestination({ id, expected, read = readBack, attempts = 3, delayMs = 2000 }) {
const record = { id, expected, actual: null, mismatches: [], attempts: 0, ok: false, checkedAt: null };
for (let i = 1; i <= attempts; i++) {
record.attempts = i;
record.actual = await read(id);
record.mismatches = record.actual
? diff(expected, record.actual)
: [{ field: "*", expected: "record exists", actual: null }];
if (record.mismatches.length === 0) break;
if (i < attempts) await new Promise((resolve) => setTimeout(resolve, delayMs));
}
record.ok = record.mismatches.length === 0;
record.checkedAt = new Date().toISOString();
return record;
}
async function main() {
const record = await verifyAtDestination({
id: process.argv[2],
expected: JSON.parse(process.argv[3] || "{}"),
});
console.log(JSON.stringify(record)); // one evidence line per run: store it
if (!record.ok) process.exitCode = 1; // non-zero exit fails the calling step
}
if (require.main === module && process.argv[2]) {
main().catch((err) => {
console.error(err.message);
process.exitCode = 1;
});
}Vì sao xảy ra
Log thực thi chỉ ghi rằng mỗi bước nhận được phản hồi không có lỗi. Nó không ghi rằng dữ liệu đúng đã đến nơi. Nếu nhà cung cấp đổi tên một trường, thu hẹp quyền hoặc ngừng gửi webhook, bước đó vẫn có thể báo thành công. Công cụ workflow chỉ nhìn vào chính nó, nên nó báo xanh trong khi dòng trong bảng tính hoặc khoản thanh toán không tồn tại.
Các bước
- Trong workflow, giữ lại ID và các trường chính vừa gửi, ví dụ số tiền và trạng thái.
- Sau bước ghi, chạy script với ID đó và các trường mong đợi ở dạng JSON.
- Script đọc bản ghi từ hệ thống đích và so sánh từng trường mong đợi.
- Nếu bản ghi chưa có, script chờ rồi thử lại, tối đa ba lần.
- Script in ra một dòng JSON gồm giá trị mong đợi, giá trị tìm thấy và thời điểm kiểm tra.
- Nếu có khác biệt, script thoát với mã 1, bước gọi nó sẽ lỗi và workflow xử lý lỗi có thể cảnh báo bạn.
Với n8n tự host, bạn có thể gọi script từ node Execute Command. Bạn cũng có thể chạy nó theo lịch với ID của các lần chạy gần đây.
Lưu ý
- Giá trị được so sánh dưới dạng chuỗi, nên
49.9và49.90bị coi là khác nhau. Hãy chuẩn hóa số và ngày trước khi so sánh. - Một số hệ thống đích cần vài giây mới đọc được bản ghi mới. Hãy tăng
attemptshoặcdelayMs, đừng bỏ bước kiểm tra. - Script chỉ đọc. Đừng thêm bước tự sửa, vì ghi đè bản ghi sẽ xóa bằng chứng về lỗi.
- Giữ API key trong biến môi trường hoặc kho credential của n8n, chỉ cấp quyền đọc. Node Execute Command có thể bị tắt mặc định và không có trên n8n Cloud.
Thấy trên: Make Community ·
Dịch vụ liên quan: Workflow tự động n8n, Make, Zapier