以下代码就像30分钟前的魅力一样,但在尝试向我的架构添加日期后,它突然停止工作,现在我总是在我的集合中添加一个空文档。
我唯一得到的是:
{ "_id" : ObjectId("564e1e7681c448840e06caea"), "__v" : 0 }
任何人都可以帮我查明代码中的错误吗?
var mongoose = require('mongoose');
Schema = mongoose.Schema;
var imagesSchema = new Schema({
auctionId: String,
name: String,
dateAdded: Date
});
mongoose.exports = imagesSchema;
var mongoose = require('mongoose');
var imagesSchema = require('./image.js');
var Image = mongoose.model('Image', imagesSchema);
Schema = mongoose.Schema;
var auctionsSchema = new Schema({
name: String,
address:
{
street: String,
number: Number,
box: String,
postalCode: Number,
city: String,
},
auctionDate: Date,
finished: Boolean,
viewingCode: String,
images: [{ type: Schema.Types.ObjectId, ref: 'Image' }]
});
mongoose.exports = auctionsSchema;
在初始化home.html页面时调用以下操作。因此,故障必须位于以下操作或上述模式设计中。
var mongoose = require('mongoose');
var auctionSchema = require('./auction.js');
mongoose.connect("mongodb://localhost/auctionsDb");
var db = mongoose.connection;
db.on('error', console.error.bind(console, "connection error"));
var Auction = db.model('Auction', auctionSchema);
//Once a connection is made to the DB, log this,
//find the db and if there are no results in the db, create a basic entry
db.once('open', function () {
console.log("auctionsDb is open...");
Auction.find().exec(function (error, results) {
if (results.length === 0) {
Auction.create({
name: "Zaal Hand in Hand",
address: {
street: "Street Anon",
number: 317,
box: "AB",
postalCode: 9490,
city: "Anonty"
},
auctionDate: new Date(),
finished: true,
viewingCode: "",
images: [{}]
});
}
});
});
答案 0 :(得分:3)
您遇到了一些问题,但主要的问题是,您可能需要将阅读module.exports
的行更改为Schema
。您没有导出{{1}}。
答案 1 :(得分:1)
始终进行错误检查。您的文档插入失败,
hashb = hashb + nb.GetHashCode() * 31;
每当你有
message: 'Cast to Array failed for value "[object Object]" at path "images"',
无一例外总是做类似于
的事情xxxx(function(error, results){ {
简单的解决方法是将图像参考无效:
xxxx(function(error, results){ {
if (error) {
console.log('save seeing error');
console.log(error);
return;
}