2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

API Connect (LoopBack) でUserモデルが作れない際の対処

Last updated at Posted at 2016-08-11

背景

API Connectのウィザードを使ってUserという名前のモデルを作ると、怒られてしまいます。

$ apic create --type model
? Enter the model name: User
? Select the data-source to attach User to: memory (memory)
? Select model's base class PersistedModel
? Expose User via the REST API? Yes
? Custom plural form (used to build REST URL):
? Common model or server only? server
Validation error: invalid ModelDefinition
 - name: is not unique
Error: The `ModelDefinition` instance is not valid. Details: `name` is not unique (value: "User")

ちなみに、userという名前であれば通ります(後述しますが、推奨されていません)。

原因

LoopBackが最初からUserというモデルを用意しており、それと名前が被るため作成することができません。

対処

LoopBackのドキュメントに書かれています。

You must create your own custom model (named something other than "User," for example "Customer" or "Client") that extends the built-in User model rather than use the built-in User model directly. The built-in User model provides a great deal of commonly-used functionality that you can use via your custom model.

https://docs.strongloop.com/display/public/LB/Using+built-in+models#Usingbuilt-inmodels-Usermodel

日本語訳すると、↓のような感じです。

ビルトインのUserモデルを直接使うのではなく、"User"以外の名前(例えば"Customer"や"Client")でUserモデルを継承したカスタムモデルを作って使わなければなりません。
ビルトインのUserモデルは、通常使われるものより多くの機能を持っているからです。

また、先頭のUを小文字にしたuserモデルは作れますが、ドキュメントで釘を刺されています。

To avoid confusion, it's also best to avoid "user" with a lowercase "u" .

https://docs.strongloop.com/display/public/LB/Extending+built-in+models#Extendingbuilt-inmodels-ExtendingmodelsusingJSON

一応訳すと、↓のような感じです。

混乱を避けるため、小文字のuを使ったuserという名前も付けないようにしましょう。

結論

というわけで、CustomerやClientという名前のモデルを作り、Userモデルを継承するのがベストな方法のようです。

所感

もうちょっと被らない名前にして欲しかったんですが……。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?