ドキュメントとサブドキュメントを別々に作成しようとしています。スキーマ:
const mongoose = require('mongoose');
require('./streets');
const PlaceSchema = new mongoose.Schema({
place1: String,
streets: [{type: mongoose.Schema.Types.ObjectId, ref: 'streetSchema'}]
});
module.exports = mongoose.model('place', PlaceSchema);
サブスキーマ:
const mongoose = require('mongoose');
const StreetSchema = new mongoose.Schema(
{
trafficLights: Number,
shops: Number,
busStops: Number,
},
{ strict: false }
);
module.exports = StreetSchema;
次に、通りを単独でインスタンス化しようとします。
const place = // ...get a certain place from DB;
const street = new Street({
trafficLights: 1,
shops: 2,
busStops: 3,
});
place.streets.push(street)
そして私は得ています
Street is not a constructor
サブスキーマを作成してインスタンス化し、それを「streets」配列にプッシュするにはどうすればよいですか?
PS - 両方のスキーマを同じファイルに入れようとしましたが、「場所」スキーマ全体をインスタンス化しないと、ネストされた配列 (通り) に到達できませんでした。
ありがとうございました。