我是做 Wix 开发的,很多活儿都是从别人建好的站点接手开始。Velo 代码因项目而异,但这五个问题总是反复出现。一旦知道该找什么,它们都不难修。

一、API 密钥写在页面代码里

这是最常见的一个,也是最严重的:

// Page code: runs in the visitor's browser
const response = await fetch("https://api.example.com/forecast?city=Dhaka", {
  headers: { Authorization: "Bearer sk_live_abc123..." },
});

页面代码在浏览器里运行,任何人都能打开开发者工具,查看网络请求,把密钥复制走。

修法:把密钥存进 Secrets Manager(仪表盘 → Developer Tools → Secrets Manager),再由后端 web 模块发起请求:

// backend/weather.web.js: runs on the server
import { Permissions, webMethod } from "wix-web-module";
import { getSecret } from "wix-secrets-backend";
import { fetch } from "wix-fetch";

export const getForecast = webMethod(Permissions.Anyone, async (city) => {
  const apiKey = await getSecret("WEATHER_API_KEY");
  const url = `https://api.example.com/forecast?city=${encodeURIComponent(city)}`;
  const response = await fetch(url, { headers: { Authorization: `Bearer ${apiKey}` } });

  if (!response.ok) throw new Error(`Forecast request failed: ${response.status}`);

  const data = await response.json();
  return { summary: data.summary, temperature: data.temperature }; // only what the page needs
});

页面代码从 backend/weather.web 导入 getForecast,像调用普通异步函数那样用它。浏览器只和你自己的后端打交道。

二、repeater 的每一项都显示同样的文字

repeater 填满了,但每张卡片显示的都是同一个标题。原因几乎总是 onItemReady 里用了 $w:

// Wrong: $w targets the first matching element on the page
$w("#repeater").onItemReady(($item, itemData) => {
  $w("#title").text = itemData.title;
});

修法:改用 $item,它指向当前这一项内部的元素:

$w("#repeater").onItemReady(($item, itemData) => {
  $item("#title").text = itemData.title;
});

还有两个相关问题:如果什么都没显示,检查每一项是不是都有唯一的字符串 _id。如果 repeater 在编辑器里还连着数据集,数据集和你的代码会互相抢,二者只用其一。

三、只在浏览器里跑的表单校验

在页面代码里检查输入对访客有好处:他们能立刻拿到清晰的提示。但页面代码是可以被跳过的,所以对任何要紧的事情,它都不能是唯一的校验。

修法:把重要的检查在 data hook 里再做一遍——它在服务器上运行,发生在数据项保存之前。在 backend/data.js 里:

export function Bookings_beforeInsert(item, context) {
  const today = new Date();
  today.setHours(0, 0, 0, 0);
  if (!item.date || new Date(item.date) < today) {
    return Promise.reject("The booking date must be today or later.");
  }
  item.email = item.email.trim().toLowerCase();
  return item;
}

本机实跑:node 原样执行 Bookings_beforeInsert,过期/空日期被 reject,合法日期通过并把邮箱 trim+转为小写

reject 掉 promise 会阻止插入。这里也是清洗数据的合适位置,比如把邮箱转成小写。

四、号称"仅会员可见"的数据,其实不然

会员页面用页面代码里的过滤器,把每位会员自己的订单显示给他:

wixData.query("Requests").eq("_owner", member._id).find();

这没写错,但单靠它保护不了任何东西。如果这个集合的权限允许任何人读取所有数据项,那么换个方式请求的人照样能拿到数据。

修法:设置集合的 Permissions(权限),让数据库层面自己去强制:

  • Read(读取): Site member author(网站会员本人)
  • Create(创建): Site member(网站会员)
  • Update(更新): Site member author(网站会员本人)
  • Delete(删除): Admin(管理员)

这样一来,上面那条查询只是图个方便,真正的保护来自权限。

五、互相等待的查询

这种写法会让页面明显变慢:

$w.onReady(async () => {
  const products = await wixData.query("Products").find();
  const reviews = await wixData.query("Reviews").find();
  // ...fill the page
});

每个 await 都在等前一个查询结束,可它们彼此并不依赖。而且每个查询都把整个集合加载了进来。

修法:把互不依赖的查询并行执行,并且只加载你要展示的部分:

$w.onReady(async () => {
  const [products, reviews] = await Promise.all([
    wixData.query("Products").limit(12).find(),
    wixData.query("Reviews").limit(5).find(),
  ]);
  // ...fill the page
});

标签页、弹窗和页面下方的内容,等访客打开或滚动到那里时再加载。

六、快速自查清单

  • 页面代码里不出现 API 密钥或任何机密
  • onItemReady 里用 $item,不是 $w
  • 重要的校验在后端 data hook 里再做一遍
  • 设好集合权限,不只是靠页面代码的过滤器
  • 互不依赖的查询放进 Promise.all,并加上 .limit()

如果这篇帮你少踩了坑,点个赞、收藏一下,下次要用时好翻。关注「山岚的运维笔记」,继续看更多前端实战。