Liverpool Liverpool - 4 months ago 24
Node.js Question

Loopback <model>.verify is not a function\n

So, I read the documentation about manage the users. I also comes from this example: link.

Then I try to make a model, called user-basic which bases on built-in model User. Here's the user-basic.json:

{
"name": "user-basic",
"base": "User",
"idInjection": true,
"properties": {},
"validations": [],
"relations": {},
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"accessType": "READ",
"permission": "ALLOW"
}
],
"methods": {}
}


Here's the user-basic.js

var config = require('../../server/config.json');
var path = require('path');


module.exports = function(UserBasic) {
UserBasic.afterRemote('create', function(ctx, member, next) {
console.log('> user.afterRemote triggered');
var options = {
type: 'email',
to: UserBasic.email,
from: 'noreply@loopback.com',
subject: 'Thanks for registering.',
template: path.resolve(__dirname, '../../server/views/verify.ejs'),
redirect: '/verified',
user: UserBasic
};
UserBasic.verify(options, function(err) {
console.log('aaaaa', member.location, err);
if (err) {
next(err);
} else {
next();
}
});

});

//send password reset link when requested
UserBasic.on('resetPasswordRequest', function(info) {
var url = 'http://' + config.host + ':' + config.port + '/reset-password';
var html = 'Click <a href="' + url + '?access_token=' +
info.accessToken.id + '">here</a> to reset your password';

UserBasic.app.models.Email.send({
to: info.email,
from: info.email,
subject: 'Password reset',
html: html
}, function(err) {
if (err) return console.log('> error sending password reset email');
console.log('> sending password reset email to:', info.email);
});
});
};


Here is my datasource:

"emailDs": {
"name": "emailDs",
"connector": "mail",
"transports": [
{
"type": "smtp",
"host": "smtp.gmail.com",
"secure": true,
"port": 465,
"tls": {
"rejectUnauthorized": false
},
"auth": {
"user": "aaa@gmail.com",
"pass": "aaa"
}
}
]
}


And this is my model-config:

"user-basic": {
"dataSource": "cloudant-pop",
"public": true,
"options": {
"emailVerificationRequired": true
}
}


I ran the server and try POST a new user via http://0.0.0.0:3008/explorer. It gives me an error:

{
"error": {
"name": "TypeError",
"status": 500,
"message": "UserBasic.verify is not a function",
"stack": "TypeError: UserBasic.verify is not a function\n at /Users/yogieputra/Desktop/backend_powercube/common/models/user-basic.js:23:23\n at Function.<anonymous> (/Users/yogieputra/Desktop/backend_powercube/node_modules/loopback/lib/model.js:207:11)\n at execStack (/Users/yogieputra/Desktop/backend_powercube/node_modules/strong-remoting/lib/remote-objects.js:480:26)\n at RemoteObjects.execHooks (/Users/yogieputra/Desktop/backend_powercube/node_modules/strong-remoting/lib/remote-objects.js:492:10)\n at phaseAfterInvoke (/Users/yogieputra/Desktop/backend_powercube/node_modules/strong-remoting/lib/remote-objects.js:652:10)\n at runHandler (/Users/yogieputra/Desktop/backend_powercube/node_modules/loopback-phase/lib/phase.js:130:5)\n at iterate (/Users/yogieputra/Desktop/backend_powercube/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)\n at Object.async.eachSeries (/Users/yogieputra/Desktop/backend_powercube/node_modules/loopback-phase/node_modules/async/lib/async.js:162:9)\n at runHandlers (/Users/yogieputra/Desktop/backend_powercube/node_modules/loopback-phase/lib/phase.js:139:13)\n at iterate (/Users/yogieputra/Desktop/backend_powercube/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)"
}
}


When I check the Database, I got the user data that I've been POST recently. Can you guys tell me what I've been missing?
Please help

Answer

Note: member.verify() or user.verify() is a prototype function.

Try this. member.verify(function.......) that is

 UserBasic.afterRemote('create', function(ctx, member, next) {
            console.log('> user.afterRemote triggered');
            var options = {
              type: 'email',
              to: UserBasic.email,
              from: 'noreply@loopback.com',
              subject: 'Thanks for registering.',
              template: path.resolve(__dirname, '../../server/views/verify.ejs'),
              redirect: '/verified',
              user: UserBasic
            };
            member.verify(options, function(err) {
              console.log('aaaaa', member.location, err);
              if (err) {
                next(err);
              } else {
                next();
              }
            });

          });