简体   繁体   中英

Questions about Node.js. SChema and models

Questions about Node.js. Schema, models and npm. I am new to node.js, and am following the steps to make schema and models in mongoose documentation.

http://mongoosejs.com/docs/

I am trying to output the model that I made in index.js using kittens.js, but I'm not sure if I'm doing correct.

There is error When I try to compile,

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

I have following codes in 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();

and this is my model schema.

    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;

    var kittySchema = new Schema({
     name: String
    });

    var Kitty = mongoose.model('Kitty', kittySchema);
    module.exports = Kitty;

Initially, I thought this is because I didn't do install npm express and mongoose, but when I tried to install them, there is error message like this

[    ..............] \ 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)

When I tried to install express and mongoose in other files, this happened as well.

Any ideas help me! Thank you!

Actually functions are hoisted on the top so first you have to define db varialble then use it.

 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM