strongloop / loopback-sandbox

A repository for reproducing LoopBack community issues.
10 stars 186 forks source link

The PersistedModel has not been correctly attached to a datasource #19

Open akhilsky opened 7 years ago

akhilsky commented 7 years ago

I am getting error: Error: Cannot call Test.create(). The create method has not been setup The PersistedModel has not been correctly attached to a datasource.

Here is my datasource.json:

{
  "db": {
    "name": "db",
    "connector": "memory"
  },
  "mongoDS": {
    "host": "localhost", 
    "port": 27017, 
    "name": "mongoDS",
    "database": "akhiltest",
    "connector": "mongodb"
  }
}

model-config.json:

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins"
    ]
  },
  "User": {
    "dataSource": "db"
  },
  "AccessToken": {
    "dataSource": "db",
    "public": false
  },
  "ACL": {
    "dataSource": "db",
    "public": false
  },
  "RoleMapping": {
    "dataSource": "db",
    "public": false,
    "options": {
      "strictObjectIDCoercion": true
    }
  },
  "Role": {
    "dataSource": "db",
    "public": false
  },
  "Test": {
    "dataSource": "mongoDS",
    "public": true
  }
}

test.js

'use strict';

module.exports = function(Test) {
Test.create({
    "first_name":"akhil"
},function(data){
    console.log(data);
})
};

test.json

{
  "name": "Test",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "first_name": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

Your help will be highly appreciated

Bug or feature request

Description of feature (or steps to reproduce if bug)

Link to sample repo to reproduce issue (if bug)

Expected result

Actual result (if bug)

Additional information (Node.js version, LoopBack version, etc)

Marwan01 commented 6 years ago

Hello. I have been having the same issue and I fixed it by getting rid of the whole Test.create({ "first_name":"akhil" },function(data){ console.log(data); }) Try using the explorer instead at localhost:3000/explorer

If that doesn't solve it, try refactoring all instances of the variable db to mongoDS or the opposite.

Marwan01 commented 6 years ago

@akhilsky lmk if this solved your issue!