Mongorito:保存(...)。然后不是一个功能

时间:2016-01-06 01:12:29

标签: node.js mongodb

我尝试了与readme.md文件中给出的示例类似的内容:

var Mongorito = require('mongorito');
var Model = Mongorito.Model;

Mongorito.connect('mongodb://localhost:27017/cr-test');

class User extends Model {
  collection() {
    return 'users';
  }
}

var user1 = new User({
  name: "James Gosling",
  email: "user1@gmail.com",
  password: "changeme"
});

user1.save().then(() => {
  console.log('User Created');
});

当我运行node --harmony server.js时出现此错误:

user1.save().then(() => {
             ^

TypeError: user1.save(...).then is not a function
    at Object.<anonymous> (...\app\server.js:24:14)
    at Module._compile (module.js:398:26)
    at Object.Module._extensions..js (module.js:405:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:430:10)
    at startup (node.js:141:18)
    at node.js:980:3

有人可以解释我怎么解决这个问题?

2 个答案:

答案 0 :(得分:1)

GitHub上的自述文件已过时,project's website现在声明:

  

基于ES6生成器的Node.j的MongoDB ODM。

     

没有回调或承诺。

它使用生成器,可以在getting started页面上找到一个示例:

'use strict';

function* saveUser () {
  var user1 = new User({
    name: "James Gosling",
    email: "user1@gmail.com",
    password: "changeme"
  });

  yield user1.save();
}

为了让生成器工作,您必须使用'use strict;'function*语法。此GitHub project还有其他示例。

此外,从Node v4开始,您不再需要生成器的--harmonynow a synonym of --es_staging)标记。

答案 1 :(得分:0)

或者你也可以这样做......但在这里你只能执行一个声明

profile = ["HandBrakeCLI","-i",input,"-o","output","-e","x264"]
cp = subprocess.Popen(profile, stderr=subprocess.PIPE, strout=subprocess.PIPE, close_fds=True)
bar = Bar(label="Encoding %s" % input, width=30, expected_size=10000, every=1)
bar.show(0)

line = ""
c = 0

while True:    
  nl = cp.stdout.read(1)
  if nl == '' and cp.poll() is not None:
     break  # Aborted, no characters available, process died.
  if nl == "\n":
     line = ""
  elif nl == "\r":
     # regex match for % complete and ETA, assuming the regex is ok.
     matches = re.match( r'.*(\d+\.\d+)\s%.*ETA\s(\d+)h(\d+)m(\d+)s', line.decode('utf-8') )

     if matches:
        print( matches.group() )
        # do something
     line = ""
  else:
     line += nl

error = cp.stderr.read()
success = "Encode done!" in error