Loopback + mongoDB:无法找到扩展用户

时间:2018-03-18 18:21:15

标签: node.js mongodb loopbackjs

我正在使用带有MongoDB连接器的Loopback 3.0。 在某个暴露的REST方法中,我需要访问当前登录的用户并对其进行一些更新。

我扩展了基本用户模型,称之为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")})

我做错了什么?

谢谢你, 马西莫

1 个答案:

答案 0 :(得分:1)

你没有给用户打电话,所以在你的回调中也是如此,你传递的是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