
[英]loopback returns unchanged document when using updateAttributes() on extended user model
[英]Loopback + mongoDB: Can't find extended user
我正在将Loopback 3.0与MongoDB连接器配合使用。 在某个地方公开的REST方法中,我需要访问当前登录的用户并对其进行一些更新。
我已经扩展了基本的User模型,将其称为appUser,登录有效,并且可以获得登录用户的令牌(在更改令牌模型以指向appUser之后)。 该模型如下:
{
"name": "appUser",
"plural": "appUsers",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"gender": {
"type": "string",
"enum": [
"M",
"F"
]
},
"birthDate": {
"type": "Date"
}
},
"validations": [],
"relations": {},
"acls": []
}
我需要访问用户个人资料,以便对其进行更新。 但是当我查询它时,结果为null。
const User = app.models.appUser;
User.findOne({
where: {
_id: ObjectId("5aae7ecd2ed1b11b1c09cf25")
}
}, (err, user) => {
if (err || res == null) {
console.log("Error updating the user ");
const error = {
"name": "Database error",
"status": 500,
"message": "Can't access the database."
}
callback(error, null, null);
} else {
//Whatever
}
});
但是,如果我在MongoDB上从Robo3T运行相同的查询,它将起作用。
db.getCollection('appUser').find({"_id": ObjectId("5aae7ecd2ed1b11b1c09cf25")})
我究竟做错了什么?
谢谢Massimo
您没有打电话给用户,情况就是这样,在回调中,您传递的是null而不是用户结果。 但是我不知道res变量来自哪里。
const User = app.models.appUser;
User.findOne({
where: {
_id: ObjectId("5aae7ecd2ed1b11b1c09cf25"),
}
}, (err, user) => {
if (err) {
console.log("Error updating the user", err); // logs the app error
const error = {
"name": "Database error",
"status": 500,
"message": "Can't access the database."
}
callback(error, null); // passes your custom error
}
console.log("APP USER", user);
callback(null, user);
});
我不怎么称呼您的回调,但我认为您可以解决这个问题。 如果仍然没有结果,请尝试将_id to id
更改_id to id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.