Node.js 为什么可以';当我从db取回小猫时,它们不会说话吗
按照指南,我显然误解了一些基本的东西。为什么shema不应用于我检索的对象 代码Node.js 为什么可以';当我从db取回小猫时,它们不会说话吗,node.js,express,mongoose,Node.js,Express,Mongoose,按照指南,我显然误解了一些基本的东西。为什么shema不应用于我检索的对象 代码 mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/local'); db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function callback() {
mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/local');
db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
// yay!
});
var kittySchema = mongoose.Schema({
name: String
})
kittySchema.methods.speak = function () {
var greeting = this.name
? "Meow name is " + this.name
: "I don't have a name"
console.log(greeting);
}
var Kitten = mongoose.model('Kitten', kittySchema)
var silence = new Kitten({ name: 'Silence' })
var fluffy = new Kitten({ name: 'fluffy' });
silence.save(function (err, silence) {
if (err) // TODO handle the error
console.log(err.message);
else
silence.speak();
});
fluffy.save(function (err, fluffy) {
if (err) // TODO handle the error
console.log(err.message);
else
fluffy.speak();
});
Kitten.find(function (err, kittens) {
if (err) // TODO handle err
console.log(kittens)
else {
for (var kitten in kittens) {
kitten.speak();
}
}
});
输出
Path\TestEx\node_modules\mongoose\lib\utils.js:413
throw err;
^
TypeError: Object 0 has no method 'speak'
at Promise.<anonymous> (Path\TestEx\app.js:66:20)
at Promise.<anonymous> (Path\TestEx\node_modules\mongoose\node_modules\mpromise\lib\promise.js:162:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (Path\TestEx\node_modules\mongoose\node_modules\mpromise\lib\promise.js:79:38)
at Promise.fulfill (Path\TestEx\node_modules\mongoose\node_modules\mpromise\lib\promise.js:92:20)
at C:\Users\Richard\WebstormProjects\TestEx\node_modules\mongoose\lib\query.js:1716:26
at model.Document.init (Path\TestEx\node_modules\mongoose\lib\document.js:243:11)
at completeMany (Path\TestEx\node_modules\mongoose\lib\query.js:1714:12)
at cb (Path\TestEx\node_modules\mongoose\lib\query.js:1677:11)
at Path\TestEx\node_modules\mongoose\lib\utils.js:408:16
Path\TestEx\node\u modules\mongoose\lib\utils.js:413
犯错误;
^
TypeError:对象0没有方法“speak”
答应。(Path\TestEx\app.js:66:20)
答应。(Path\TestEx\node\u modules\mongoose\node\u modules\mpromise\lib\promise.js:162:8)
在Promise.EventEmitter.emit(events.js:95:17)
在Promise.emit(Path\TestEx\node\u modules\mongoose\node\u modules\mpromise\lib\Promise.js:79:38)
在Promise.fulfill(Path\TestEx\node\u modules\mongoose\node\u modules\mpromise\lib\Promise.js:92:20)
在C:\Users\Richard\webstorprojects\TestEx\node\u modules\mongoose\lib\query.js:1716:26
在model.Document.init(Path\TestEx\node\u modules\mongoose\lib\Document.js:243:11)
在completeMany(Path\TestEx\node\u modules\mongoose\lib\query.js:1714:12)
在cb(Path\TestEx\node\u modules\mongoose\lib\query.js:1677:11)
在Path\TestEx\node\u modules\mongoose\lib\utils.js:408:16
您的JavaScript
中有一个错误。检查一下。它迭代对象的字段,而不是数组。因此,与其为……而。。。在
循环中,您应该使用:
for(变量i=0;i
for (var i = 0; i < kittens.length; i++) {
kittens[i].speak();
}