我试图在mongoose网站上开始使用这个小猫示例,但我不断收到此错误说:
process.nextTick(function(){throw err;}) ^ TypeError:undefined不是函数
我的代码直接从网站上的示例中复制,但不起作用。
var express = require('express');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/db');
var app = express();
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
var kittySchema = mongoose.Schema({
name: String
})
var Kitten = mongoose.model('Kitten', kittySchema)
var silence = new Kitten({ name: 'Silence' })
console.log(silence.name) // 'Silence'
// NOTE: methods must be added to the schema before compiling it with mongoose.model()
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 fluffy = new Kitten({ name: 'fluffy' });
fluffy.speak() // "Meow name is fluffy"
});
app.get('/', function(req, res) {
res.send('hello world');
});
app.get('/secret', function(req, res) {
res.send('secret page');
});
var port = process.env.PORT || 1337;
app.listen(port, function() {
console.log('http://127.0.0.1:' + port + '/');
});
对不起,如果答案很明显,但我刚刚开始使用Node.js并需要一些帮助来解决它。
提前谢谢。
更新:完整的错误消息
/Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/lib/server.js:274
process.nextTick(function() { throw err; })
^
TypeError: undefined is not a function
at NativeConnection.<anonymous> (/Users/joseph/Documents/Atom-files/test/app.js:18:17)
at NativeConnection.g (events.js:199:16)
at NativeConnection.emit (events.js:104:17)
at open (/Users/joseph/Documents/Atom-files/node_modules/mongoose/lib/connection.js:485:10)
at NativeConnection.Connection.onOpen (/Users/joseph/Documents/Atom-files/node_modules/mongoose/lib/connection.js:494:5)
at /Users/joseph/Documents/Atom-files/node_modules/mongoose/lib/connection.js:453:10
at /Users/joseph/Documents/Atom-files/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:59:5
at /Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/lib/db.js:206:5
at connectHandler (/Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/lib/server.js:272:7)
at g (events.js:199:16)
at emit (events.js:107:17)
at /Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:374:23
at /Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:730:13
at Callbacks.emit (/Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:84:3)
at null.messageHandler (/Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:219:23)
at Socket.<anonymous> (/Users/joseph/Documents/Atom-files/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:259:22)
14 May 21:02:09 - [nodemon] app crashed - waiting for file changes before starting...
答案 0 :(得分:4)
您需要两次编译架构,其中一次是在定义方法之前。这一行特别是:
var Kitten = mongoose.model('Kitten', kittySchema)
在用kittySchema.methods
编译mongoose.model
之前,您需要等到定义好TypeError: undefined is not a function
之后。你自己的代码评论告诉你不要这样做:)
此外,为了将来参考,您粘贴的has no method 'speak'
错误并没有多大用处,但它下方的调用堆栈却指出了蓬松的Investigations
。
答案 1 :(得分:0)
clean_data <- data [,colSums(data)!=0] & [rowSums(data)!=0,]
Given this matrix:
0 1 0 2 3;
0 0 0 0 1;
0 0 0 0 0;
1 0 0 2 0;
I need this matrix:
0 1 2 3;
0 0 0 1;
1 0 2 0;