node.js - Using build in model accessToken not working in loopback -
i'm trying begin programming api android application , wanted use node.js + loopback this. i'm running trouble testing/ learning language.
following code should generate new users in database(and does) when try login user no accesstoken in created printen console. idea i'm doing wrong?
my test code: create-test-data.js
var app = require('./app'); var datasource = app.datasources.mysql; var loopback = require('loopback'); var user = loopback.user; // getting user model var accesstoken = loopback.accesstoken; // getting accesstokenmodel /* initializing database done this. if exist clean it. datasource.automigrate('user', function (err) {}); datasource.automigrate('account', function (err) {}); datasource.automigrate('accesstoken', function (err) {}); */ //creating users in mysql database user.create({username: 'timbo', email: 'test@gmail.com', password: 'monkey123'} , function(err, user) {console.log(user);}); user.create({username: 'timbo2', email: 'test2@gmail.com', password: 'monkey123'} , function(err,user) {console.log(user);}); user.create({username: 'timbo3', email: 'test3@gmail.com', password: 'monkey123'} , function(err,user) {console.log(user);}); user.login({username: 'timbo', password: 'monkey123'}, function(err, accesstoken) {console.log("this token: " + accesstoken);}); //no accesstoken created / saved in database
datasource.json
{ "db": { "defaultfortype": "db", "connector": "mysql", "host": "127.0.0.1", "database": "test", "user": "root", "password": "warcraft" }, "push": { "defaultfortype": "push", "connector": "loopback-push-notification", "installation": "installation", "notification": "notification", "application": "application" }, "mail": { "defaultfortype": "mail", "connector": "mail" }, "mysql": { "connector": "mysql", "host": "127.0.0.1", "database": "test", "user": "root", "password": "warcraft" } }
models.json
{ "email": { "options": { "base": "email" }, "datasource": "mail", "public": false }, "user": { "options": { "base": "user", "relations": { "accesstokens": { "model": "accesstoken", "type": "hasmany", "foreignkey": "userid" } } }, "datasource": "mysql", "public": true }, "accesstoken": { "options": { "base": "accesstoken" }, "datasource": "mysql", "public": true }, "application": { "options": { "base": "application" }, "datasource": "db", "public": true }, "acl": { "options": { "base": "acl" }, "datasource": "db", "public": false }, "rolemapping": { "options": { "base": "rolemapping" }, "datasource": "db", "public": false }, "role": { "options": { "base": "role", "relations": { "principals": { "type": "hasmany", "model": "rolemapping", "foreignkey": "roleid" } } }, "datasource": "db", "public": false }, "scope": { "options": { "base": "scope" }, "datasource": "db", "public": false }, "push": { "options": { "base": "push", "plural": "push" }, "datasource": "push" }, "installation": { "options": { "base": "installation" }, "datasource": "db", "public": true }, "notification": { "options": { "base": "notification" }, "datasource": "db", "public": true }, "product": { "properties": { "email": { "type": "string" }, "level": { "type": "number" }, "create": { "type": "date" }, "modified": { "type": "date" } }, "public": true, "datasource": "db", "plural": "products" }, "account": { "properties": { "email": { "type": "string" }, "level": { "type": "number" }, "created": { "type": "date" }, "modified": { "type": "date" } }, "public": true, "datasource": "mysql", "plural": "accounts" } }
console output
{ username: 'timbo', email: 'test@gmail.com', password: '$2a$10$972dfwmouohkj5thfbchc.ipcnaw27ccphmrkw17uslutachyzf0g', realm: undefined, emailverified: undefined, verificationtoken: undefined, credentials: [], challenges: [], status: undefined, created: undefined, lastupdated: undefined, id: undefined } { username: 'timbo2', email: 'test2@gmail.com', password: '$2a$10$1pesixaoiqq8umozzey86oqkxopfu.ax2/nwc1olgjqhpp9ozdpdw', realm: undefined, emailverified: undefined, verificationtoken: undefined, credentials: [], challenges: [], status: undefined, created: undefined, lastupdated: undefined, id: undefined } { username: 'timbo3', email: 'test3@gmail.com', password: '$2a$10$x3fdv2dl6kjuj69dqr.jmevdqimzven7nnjp5txag54b4tpzz4lgw', realm: undefined, emailverified: undefined, verificationtoken: undefined, credentials: [], challenges: [], status: undefined, created: undefined, lastupdated: undefined, id: undefined } token: undefined token err: error: er_bad_field_error: unknown column 'ttl' in 'field list'
you have attach user-related models datasource first:
loopback.user.attachto(datasource); loopback.accesstoken.attachto(datasource); loopback.role.attachto(datasource); loopback.acl.attachto(datasource);
and define relationship between user
, accesstoken
:
loopback.user.hasmany(loopback.accesstoken, {as: 'accesstokens'});
when creating test data, should wait user.create
finish before calling user.login
. (rember, node.js asynchronous.)
user.create( {username: 'timbo', email: 'test@gmail.com', password: 'monkey123'}, function(err, user) { // todo: handle err != null user.login( {username: 'timbo', password: 'monkey123'}, function(err, accesstoken) { console.log("this token: " + accesstoken); }); });
Comments
Post a Comment