"label" 在流星简单模式中的用法

"label" usage in meteor simple schema

我已经在 meteor 简单模式中多次看到这种 "label" 用法。只是不知道为什么我们需要这样一个字段。

 const Product = new SimpleSchema({   _id: {
     type: String,
     label: "Product ID"   } })

谢谢

德里克

这适用于 Autoform 包:https://github.com/aldeed/meteor-autoform

所以除非你正在使用它,否则你不需要它。

IMO标签是字段的可读名称,它有助于代码更语义化。它在调试时也有帮助,例如,如果您有一个架构字段,如:

// ...
appId: {
  type: String,
},
// ...

然后,如果您在插入时没有提供 appId 值,您将收到此错误 Error: App id is required。可能很难知道哪里出了问题,因为 SimpleSchema 会自动重新格式化字段名称。如果您提供标签字段:

// ...
appId: {
  type: String,
  label: 'App Id of the document',
},
// ...

那么错误信息会是:Error: App Id of the document is required,有了这个信息更容易找到问题。

如果您只使用简单模式,label 将纯粹用于显示更人性化的 readable/understandable 错误消息格式,如@Khang 所回答。

如果您使用自动表单生成基于简单模式的 for,理想情况下,该字段的标签将根据简单模式中定义的内容自动生成。但是如果你想更详细地展示它,你可以通过专门定义标签来覆盖它。

例如:

userName :{
 type: String,
...
}

将生成一个带有输入文本框的表单。该输入框的标签默认为"User Name"

userName:{
type: String,
label: "someTextHere",
...
}

将生成一个输入文本框。此输入框的标签现在将是 "someTextHere" 而不是 "User Name"