Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点orm2返回find()结果_Javascript_Node.js_Orm - Fatal编程技术网

Javascript 节点orm2返回find()结果

Javascript 节点orm2返回find()结果,javascript,node.js,orm,Javascript,Node.js,Orm,在我的配置文件模型中,我有以下方法: // Check login and return object checkLogin: function(username, password) { ProfileObj = Profile.find({username: username, password: password}).one(function(err, profile){ return profile;

在我的配置文件模型中,我有以下方法:

// Check login and return object
checkLogin: function(username, password) {
        ProfileObj = Profile.find({username: username, password: password}).one(function(err, profile){
                         return profile;
                    });

        console.log(ProfileObj);
        return ProfileObj;
}
要调用此方法,我使用:

profile = req.models.profile();
ProfileObj = profile.checkLogin('test', 'test');
不幸的是,我没有得到我想要的对象,我得到的是模型,而不是实例。find()回调中的“profile”变量是我想要的,但没有返回它。相反,ProfileObj是整个模型的一部分

{ find: [Function],
  only: [Function],
  limit: [Function],
  skip: [Function],
  offset: [Function],
  order: [Function],
  orderRaw: [Function],
  count: [Function],
  remove: [Function],
  first: [Function],
  last: [Function],
  each: [Function],
  run: [Function],
  success: [Function],
  fail: [Function],
  all: [Function],
  where: [Function],
  one: [Function],
  beforeCreate: [Function],
  afterCreate: [Function],
  beforeSave: [Function],
  afterSave: [Function],
  beforeValidation: [Function],
  beforeRemove: [Function],
  afterRemove: [Function],
  afterLoad: [Function],
  afterAutoFetch: [Function],
  extendsTo: [Function],
  model:
   { [Function]
     allProperties:
      { username: [Object],
        name: [Object],
        email: [Object],
        password: [Object],
        id: [Object] },
     settings: { set: [Function], get: [Function], unset: [Function] },
     drop: [Function],
     sync: [Function],
     get: [Function],
     find: [Function],
     all: [Function],
     one: [Function],
     count: [Function],
     aggregate: [Function],
     exists: [Function],
     create: [Function],
     clear: [Function],
     beforeCreate: [Function],
     afterCreate: [Function],
     beforeSave: [Function],
     afterSave: [Function],
     beforeValidation: [Function],
     beforeRemove: [Function],
     afterRemove: [Function],
     afterLoad: [Function],
     afterAutoFetch: [Function],
     hasOne: [Function],
     hasMany: [Function],
     extendsTo: [Function] },
  options:
   { only: [ 'id', 'username', 'name', 'email', 'password' ],
     id: [ 'id' ],
     table: 'profile',
     driver:
      { config: [Object],
        opts: [Object],
        customTypes: {},
        query: [Object],
        db: [Object],
        aggregate_functions: [Object],
        uid: '2d7089091970609caded514e621b51a7' },
     conditions: { username: 'test', password: 'test' },
     associations: [],
     limit: undefined,
     order: null,
     merge: null,
     offset: undefined,
     newInstance: [Function] } }
您能告诉我如何正确返回find()回调中的“profile”吗


谢谢

这是不可能的,因为node.js使用了异步代码。

您可以在您的模型中创建一个方法,如下所示:

models.profile = db.define('profile', {
    id: { type: 'serial', key: true },
    another_column: { type: 'text' }
}, {
   methods: {
       checkLogin: function (username, password) {
           models.profile.one({ username, password }, (err, user) => {
               return user;
           });
       }
    }
}
从理论上讲,由于您在表单提交中有用户名和密码,因此可以执行以下操作:

req.models.profile.find({ username: username }, (err, user) => {
    return user.checkLogin(user.username, password);
});

祝你好运

这是可能的,您需要使用ORMs方法。见我下面的答复。