Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.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 产量显然不适合猫鼬_Javascript_Node.js_Mongodb_Ecmascript 6 - Fatal编程技术网

Javascript 产量显然不适合猫鼬

Javascript 产量显然不适合猫鼬,javascript,node.js,mongodb,ecmascript-6,Javascript,Node.js,Mongodb,Ecmascript 6,我试图使用es6

我试图使用
es6
功能对mongodb进行同步查询,当我在测试中调用我的函数时,这显然不起作用

我的功能代码如下:

'use strict';

let Notifications, lodash;

lodash = require('lodash')

Notifications = require('../../api/notifications/notifications.model');

function *syncQuery (email, kind, notificationIds) {
  let ids, query, remains;
  query = {
    userEmail: email,
    'notifications.kind' : kind
  };
  return yield Notifications.findOne(query, {}, {lean: true}, (err, usr) => {
    let toNotify;

    if (err) {
      console.log('Error:', err);
      reject(err);
    }
    toNotify = lodash.difference(notificationIds, usr.notifications.id);
    return toNotify;
  });
}

module.exports = syncQuery;
  it('should be synchronous', done => {
    let remains = syncQuery(n.user, 'budget', [9, 71, 24, 90, 5]);
    console.log(remains);
    remains.next();
    console.log('we'r here(?)');
    console.log(remains);
    done();
  })
我的模块规格是:

'use strict';

let Notifications, lodash;

lodash = require('lodash')

Notifications = require('../../api/notifications/notifications.model');

function *syncQuery (email, kind, notificationIds) {
  let ids, query, remains;
  query = {
    userEmail: email,
    'notifications.kind' : kind
  };
  return yield Notifications.findOne(query, {}, {lean: true}, (err, usr) => {
    let toNotify;

    if (err) {
      console.log('Error:', err);
      reject(err);
    }
    toNotify = lodash.difference(notificationIds, usr.notifications.id);
    return toNotify;
  });
}

module.exports = syncQuery;
  it('should be synchronous', done => {
    let remains = syncQuery(n.user, 'budget', [9, 71, 24, 90, 5]);
    console.log(remains);
    remains.next();
    console.log('we'r here(?)');
    console.log(remains);
    done();
  })
当我运行测试时,输出如下:

{}
we'r here(?)
{}
与JS+ES6相关的人可以帮助我。我的最后一个方法是对我的函数返回一个承诺,但这对我的职责没有帮助,我真的需要做这项工作


PS:可能是因为我的语法不是最好的(可能我使用的不是ES6的最佳实践),也可能是因为我的英语水平(可能是一些拼写错误)。因此,感谢您阅读此

。如果您使用的是NodeJS,那么在某个时候需要使用Async/promissions。现在,您只需
生成一个未执行的查询。或者,您正在寻找类似的产品。我认为您不了解
yield
的功能。您无法将任何异步转换为同步(相信我,您甚至不想)。首先了解承诺和ES7异步/等待。那你就可以使用发电机了。