Node Mongoose TypeError:object不是Array.map和Model.create的函数

时间:2023-01-16 15:46:51

I'm getting some unexpected behavior out of Mongoose: when I use Model.create as an argument in a mapping function, I receive an error

我从Mongoose中得到了一些意想不到的行为:当我在映射函数中使用Model.create作为参数时,我收到一个错误

variables.map(Variable.create);

TypeError: object is not a function
  at Array.forEach (native)
  at Array.map (native)

but when I wrap the Model.create in an anonymous function, I don't receive the error:

但是当我在一个匿名函数中包装Model.create时,我没有收到错误:

variables.map(function(variable) {
  return Variable.create(variable);
});

What gives?

Using "node": "0.10.33" and "mongoose": "3.8.25"

使用“node”:“0.10.33”和“mongoose”:“3.8.25”

1 个解决方案

#1


Ah, you've stumbled into the world of Javascript objects and their methods/properties.

啊,你偶然发现了Javascript对象及其方法/属性的世界。

The short answer is internally the method create uses other object properties/methods from Variable. When you pass Variable.create to the mapping function it passes a reference to create directly and so the prototype chain is now broken. If you really want to do it that way you could use bind to bind it back to it's parent object:

简短的回答是内部方法create使用Variable中的其他对象属性/方法。当您将Variable.create传递给映射函数时,它会直接传递一个引用,因此原型链现在已经被破坏了。如果你真的想这样做,你可以使用bind将它绑定回它的父对象:

variables.map(Variables.create.bind(Variables));

var objA = {
  word: 'bar',
  say: function() {
    alert(this.word);
  }
};

var objB = {
  word: 'foo',
  say: function() {
    alert(this.word);
  }
};

var say = objA.say;
var bound = objA.say.bind(objB);

objA.word; // bar
objA.say(); // bar

objB.word; // foo
objB.say(); // foo

say(); // undefined --> this.word
bound(); // foo

// bonus fun
word = 'hello'; // 'this' is the global scope here so this.word === word
say(); // hello

For the long answer I would recommend reading You Don't Know JS: this & Object Prototypes.

对于长篇答案,我建议阅读“你不了解JS:这个和对象原型”。

#1


Ah, you've stumbled into the world of Javascript objects and their methods/properties.

啊,你偶然发现了Javascript对象及其方法/属性的世界。

The short answer is internally the method create uses other object properties/methods from Variable. When you pass Variable.create to the mapping function it passes a reference to create directly and so the prototype chain is now broken. If you really want to do it that way you could use bind to bind it back to it's parent object:

简短的回答是内部方法create使用Variable中的其他对象属性/方法。当您将Variable.create传递给映射函数时,它会直接传递一个引用,因此原型链现在已经被破坏了。如果你真的想这样做,你可以使用bind将它绑定回它的父对象:

variables.map(Variables.create.bind(Variables));

var objA = {
  word: 'bar',
  say: function() {
    alert(this.word);
  }
};

var objB = {
  word: 'foo',
  say: function() {
    alert(this.word);
  }
};

var say = objA.say;
var bound = objA.say.bind(objB);

objA.word; // bar
objA.say(); // bar

objB.word; // foo
objB.say(); // foo

say(); // undefined --> this.word
bound(); // foo

// bonus fun
word = 'hello'; // 'this' is the global scope here so this.word === word
say(); // hello

For the long answer I would recommend reading You Don't Know JS: this & Object Prototypes.

对于长篇答案,我建议阅读“你不了解JS:这个和对象原型”。