簡體   English   中英

貓鼬問題:未定義架構

[英]Mongoose problem: schema not defined

大家好,我一直在嘗試使用貓鼬和node,但是即使是最簡單的運行,我也有一些問題...我有以下代碼:

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

var db = mongoose.connect('mongodb://localhost/db');

var User = new Schema({
  email: {
    type: String,
    index: { unique: true }
  },
  name: String,
  lastseen: Date,
  isonline: Boolean,
  hashed_password: String,
  salt: String
});

mongoose.model('User', User);

var User = db.model('User');

var u = new User();
u.name = 'Foo';

u.save(function() {
    User.find().all(function(arr) {
        console.log(arr);
        console.log('length='+arr.length);
    });
});

應該運行,因為它是示例代碼...但是我遇到了這個錯誤:

node.js:181
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
ReferenceError: Schema is not defined
    at Object.<anonymous> (myfile.js:12:1)
    at Module._compile (module.js:420:26)
    at Object..js (module.js:426:10)
    at Module.load (module.js:336:31)
    at Function._load (module.js:297:12)
    at Array.<anonymous> (module.js:439:10)
    at EventEmitter._tickCallback (node.js:173:26)

你們當中有人知道這是怎么回事嗎? 謝謝。

您正在使用哪個版本的貓鼬?

我遇到了貓鼬<1.1.0的類似問題,該問題已得到解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM