【问题标题】:Loopback API Error while creating the profile model创建配置文件模型时出现环回 API 错误
【发布时间】:2019-09-06 18:29:53
【问题描述】:

我正在尝试在环回中创建配置文件模型,但它显示错误。

错误详情 请求 POST /api/Users 的未处理错误:错误:无法调用 Profile.create()。尚未设置创建方法。 PersistedModel 未正确附加到数据源!

 'use strict';

    var loopback = require('loopback');
    var boot = require('loopback-boot');

    var app = module.exports = loopback();

    app.start = function() {
      // start the web server
      return app.listen(function() {
        app.emit('started');
        var baseUrl = app.get('url').replace(/\/$/, '');
        console.log('Web server listening at: %s', baseUrl);
        if (app.get('loopback-component-explorer')) {
          var explorerPath = app.get('loopback-component-explorer').mountPath;
          console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
        }
      });
    };

    // Bootstrap the application, configure models, datasources and middleware.
    // Sub-apps like REST API are mounted via boot scripts.
    boot(app, __dirname, function(err) {
      if (err) throw err;

      // start the server if `$ node server.js`
      if (require.main === module)
        app.start();
    });
    console.log(Object.keys(app.models));

    app.models.User.afterRemote('create',(ctx,user,next)=>{

     console.log("The new User is ",user);


    app.models.Profile.create({
      first_name :user.username,
       created_at :new Date(),
       userId: user.id
    }, (err,result)=>{
      if(!err && result)
      {
        console.log("Created new profile !", result);
      }
      else{
        console.log("There is an error ",err);
      }
      next();
    });

    });

Prfile.JSON 文件

{
  "name": "Profile",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "first_name": {
      "type": "string"
    },
    "last_name": {
      "type": "string"
    },
    "birth_date":{
      "type":"date"
    },
    "created_at": {
      "type": "date"
    },
    "age": {
      "type": "number"
    },
    "history":{
      "type":["object"]
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

**这个文件是profile.js文件**

'use strict';

module.exports = function(Profile) {

};

【问题讨论】:

    标签: javascript node.js loopbackjs


    【解决方案1】:

    也许您仍然需要将模型绑定到 model-config.json 文件中的数据源,如下所示:

    datasources.json

      "rdb": {
        "host": "localhost",
        "port": 3306,
        "database": "asat",
        "password": "12345",
        "name": "rdb",
        "user": "admin",
        "connector": "mysql"
      }
      ...
    

    model-config.json

      "Profile": {
        "dataSource": "rdb",
        "public": true
      }
      ...
    

    【讨论】:

      【解决方案2】:

      原因可以是以下任何一种

      • 忘记在 model-config.json 文件中添加模型名称
      • 有时当环回服务器启动时,模型没有正确附加到服务器对象。如果模型未正确附加,我们将无法使用 app 对象使用模型,并且重新启动服务器即可解决此问题。
      • 模型配置文件中未正确提及数据源

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-23
        • 2016-10-24
        • 1970-01-01
        • 2017-10-12
        • 2019-03-18
        • 2016-01-30
        • 1970-01-01
        • 2016-02-26
        相关资源
        最近更新 更多