2016-12-24 5 views
0
restraunt.json file 

`{ 
    "name": "restraunt", 
    "base": "PersistedModel", 
    "idInjection": true, 
    "options": { 
    "validateUpsert": true 
    }, 
    "properties": { 
    "name": { 
     "type": "string", 
     "required": true 
    }, 
    "location": { 
     "type": "string", 
     "required": true 
    } 
    }, 
    "validations": [], 
    "relations": {}, 
    "acls": [], 
    "methods": {} 
}` 

restraunt.js file 

`module.exports = function(Restraunt) { 
    Restraunt.find({where:{id:1}}, function(data) { 
     console.log(data); 
    }) 
};` 

model-config.json file 
`"restraunt": { 
    "dataSource": "restrauntManagement" 
    }` 

datasources.json file 
`{ 
    "db": { 
    "name": "db", 
    "connector": "memory" 
    }, 
    "restrauntManagement": { 
    "host": "localhost", 
    "port": 0, 
    "url": "", 
    "database": "restraunt-management", 
    "password": "restraunt-management", 
    "name": "restrauntManagement", 
    "user": "rohit", 
    "connector": "mysql" 
    } 
}` 

が、 restraunt.jsファイルから「見つける」ことができません。エラーが発生します。 「エラー:。。(restraunt.findを呼び出すことはできません)findメソッドが設定されていないPersistedModelは正しくデータソースに接続されていない」ことができない:永続モデルが正しくデータソースに接続されていなかった私は、SQL DBが正しく設定されている意味エクスプローラから、ポストを、取得置くことができる午前

答えて

0

再びMySQLのコネクタをインストールしてください:

npm i -S loopback-connector-mysql 

を取りますあなたのdatasources.jsonを見てください。mysqlのポートが間違っている可能性があり、デフォルトのポートは3306です。また、localhostを0.0.0.0に変更することもできます。

"restrauntManagement": { 
    "host": "localhost", /* if you're using docker, you need to set it to 0.0.0.0 instead of localhost */ 
    "port": 0, /* default port is 3306 */ 
    "url": "", 
    "database": "restraunt-management", 
    "password": "restraunt-management", 
    "name": "restrauntManagement", 
    "user": "rohit", 
    "connector": "mysql" 
    } 

モデル-config.jsonでなければなりません:

"restraunt": { 
    "dataSource": "restrauntManagement" /* this name must be the same name in datasources object key (in your case it is restrauntManagement not the connector name which is mysql) */ 
    } 

ます。また、レストランのモデルのマイグレーションを実行する必要があります。

サーバーの/ boot /にmigration.jsを作成し、これを追加します。

'use strict'; 

module.exports = function(server) { 
    var mysql = server.dataSources.mysql; 

    mysql.autoupdate('restraunt'); 
}; 

あなたが使用するすべてのモデルを移行する必要があります。それらをデータソースにアタッチする場合は、デフォルトモデル(ACL、AccessTokenなど)も移行する必要があります。

また、docsでは、システム(その時点で)が完全にロードされていないため、model.jsファイル内で操作を実行することはできません。実行する必要のある操作は、システムが完全にそこにロードされているため、/ bootディレクトリ内の.jsファイル内になければなりません。システムがロードされているため、リモートメソッド内で操作を実行することもできます。

+0

は、migration.jsにコードを追加しました。私はserver.dataSources.mysqlを置き換えました。 server.dataSources.restrauntManagmentを使用します。ので、私のSQLデータベースの名前thats。しかし、私がserver.dataSources.mysqlを使用したときにも同じエラー –

+0

が表示されます。私はconsole.log(server.dataSources.mysql)の 'undefined'を表示しました –

+0

私はあなたがrestrauntではなくレストランを意味すると信じています。 – brlebtag

関連する問題