关于Node.js的问题架构,模型和npm。 我是node.js的新手,我正在遵循在mongoose文档中制作模式和模型的步骤。
我正在尝试使用kittens.js输出我在index.js中创建的模型,但我不确定我是否正确。
当我尝试编译时出现错误,
C:\Atom\initial\index.js:11
db.on('error', console.error.bind(console, 'connection_error'));
^
TypeError: Cannot read property 'on' of undefined
at Object.<anonymous> (C:\Atom\initial\index.js:11:3)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
我在index.js中有以下代码。
var express = require('express');
var mongoose = require('mongoose');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var Kitty = require('./models/kittens.js');
mongoose.connect("my database");
db.on('error', console.error.bind(console, 'connection_error'));
db.once('open', function(){
console.log("Connected!");
});
var db = mongoose.connection;
var Kitty = db.model('Kitty', kittySchema);
var silence = new Kitty({name: 'Silence'});
console.log(silence.name);
db.close();
这是我的模型架构。
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var kittySchema = new Schema({
name: String
});
var Kitty = mongoose.model('Kitty', kittySchema);
module.exports = Kitty;
最初,我认为这是因为我没有安装npm express和mongoose,但是当我尝试安装它们时,会出现这样的错误消息
[ ..............] \ fetchMetadata: verb afterAddevents.js:160ps://registry.npmjs.org/mime-types
throw er; // Unhandled 'error' event
^
Error: This socket is closed
at WriteStream.Socket._writeGeneric (net.js:678:19)
at WriteStream.Socket._write (net.js:729:8)
at doWrite (_stream_writable.js:333:12)
at writeOrBuffer (_stream_writable.js:319:5)
at WriteStream.Writable.write (_stream_writable.js:246:11)
at WriteStream.Socket.write (net.js:656:40)
at Object.Gauge._doRedraw (C:\Program Files\nodejs\node_modules\npm\node_modules\npmlog\node_mo
dules\gauge\index.js:208:26)
at Object.Gauge.hide (C:\Program Files\nodejs\node_modules\npm\node_modules\npmlog\node_modules
\gauge\index.js:162:8)
at EventEmitter.log.clearProgress (C:\Program Files\nodejs\node_modules\npm\node_modules\npmlog
\log.js:127:14)
at EventEmitter.log.disableProgress (C:\Program Files\nodejs\node_modules\npm\node_modules\npml
og\log.js:88:8)
当我尝试在其他文件中安装express和mongoose时,也会发生这种情况。
任何想法对我有帮助!谢谢!
答案 0 :(得分:0)
实际上,函数在顶部悬挂,因此首先必须定义db varialble然后使用它。
var express = require('express');
var mongoose = require('mongoose');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var Kitty = require('./models/kittens.js');
mongoose.connect("my database");
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection_error'));
db.once('open', function(){
console.log("Connected!");
});
var Kitty = db.model('Kitty', kittySchema);
var silence = new Kitty({name: 'Silence'});
console.log(silence.name);
db.close();