gpt4 book ai didi

node.js - 在loopbackjs应用程序上注册新用户

转载 作者:太空宇宙 更新时间:2023-11-03 22:08:27 25 4
gpt4 key购买 nike

这个问题我已经查过了。但答案与我正在寻找的不太相符。 Extend built-in User model to support more properties and behaviors in loopback

我正在尝试做的事情:在我的测试应用程序上注册一个新用户。我只需要在点击注册时将数据保存到mysql数据库即可。环回文档中用于注册新用户的大多数示例都来自启动脚本。并且这里的示例没有将其保存到数据库 - https://github.com/strongloop/loopback-example-user-management 。这更多是一个问题,为什么我无法将其保存到数据库中,而不能保存到寄存器或用户模型上。但模型 userAccount 是从 User 基础模型扩展而来的。所以我不确定这是否有不同的工作方式。我仍然不完全熟悉 Loopback 框架,因此非常感谢任何帮助。谢谢。

背景将环回 3.0 与 NodeJS 8.1.0 结合使用。使用的连接器 - Loopback-connector-mysql 连接到 Azure 上托管的 MySQL 数据库。

使用loopback创建的POST/userAccounts(默认API端点)可以连接到数据库并保存用户。我只是在使用远程方法 POST/register 时遇到了问题。

问题我收到函数未找到错误。截屏 - Screenshot of error

/server/model.config

{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"User": {
"dataSource": "db",
"public": false
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"userCredential": {
"dataSource": "db",
"public": false
},
"userIdentity": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false,
"options": {
"strictObjectIDCoercion": true
}
},
"Role": {
"dataSource": "db",
"public": false
},
"userAccount": {
"dataSource": "mysqlDB",
"public": true,
"options": {
"remoting": {
"sharedMethods": {
"*": true
}
}
}
},
"job": {
"dataSource": "db",
"public": true
},
"jobSeekerProfile": {
"dataSource": "db",
"public": true
},
"userRegister":{
"dataSource":null,
"public":false
}
}

/server/models/userAccount.json

{
"name": "userAccount",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"username": {
"type": "string",
"required": true
},
"password": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {
"register": {
"accepts": {
"arg": "profile",
"type": "userRegister",
"description": "User profile details",
"required": true,
"http": {
"source": "body"
}
},
"returns": {
"arg": "result",
"type": "object",
"root": true,
"description": "This method indicates user registration status."
},
"description": "Register new user",
"http": {
"verb": "post"
}
}
}
}

/server/models/userAccount.js

'use strict';

module.exports = function(usersHR) {
class userAccount {
async register(userRegister, req, callback) {
var today = new Date();
var newUserAccount = new userAccount({
username: userRegister.username,
password: userRegister.password,
joinDate: today.toUTCString(),
lastLogin: today.toUTCString(),
realm: 'jobseeker',
email: userRegister.email,
});
userAccount.save(newUserAccount, function(err) {
if (err) {
console.log(err);
} else {
console.log('User registered');
}
});

callback(null, 'Registered');
}
}

var cUserAccount = new userAccount(usersHR);

usersHR.register = function() {
cUserAccount.register(...arguments);
};
};

/common/models/userRegister.json

{
"name": "userRegister",
"properties": {
"username": {
"type": "string",
"description" : "username of user"
},
"email":{
"type":"string",
"description":"email of user"
},
"password": {
"type": "string",
"description" : "password of user"
},
"realm":{
"type":"string",
"description":"realm of user"
}
},
"hidden": [
"id"
]
}

最佳答案

What I am trying to do: Register a new user on my test application. I just need to save the data to mysql database when I click register.

默认的User模型允许$everyone创建用户模型。这意味着创建用户最简单的行为就是发布/api/Users/create

Most of the examples on the loopback documentation to register new user, is from a boot script. And the example here does not save it to database - https://github.com/strongloop/loopback-example-user-management.

这是因为连接器是内存中连接器,您可以交换任何持久数据源,并且用户将持续存在。

This is more of a problem on why I am not able to save it to database and not so much on the register or user models. But the model userAccount extends from the User base model. So I am not sure if this works differently. I am still not completely familiar with Loopback framework, so any help is greatly appreciated. Thanks.

为了解决您的错误,您正在调用 userAccount(您在函数中创建的类)上的 save 方法,您可能想在 上调用它>usersHR.

旧:

userAccount.save(newUserAccount, function(err) {
if (err) {
console.log(err);
} else {
console.log('User registered');
}
});

新:

usersHR.save(newUserAccount, function(err) {
if (err) {
console.log(err);
} else {
console.log('User registered');
}
});

关于node.js - 在loopbackjs应用程序上注册新用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49420625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com