嵌入另一个猫鼬模式

时间:2019-12-02 17:09:26

标签: node.js mongodb mongoose

我正试图将模型嵌入猫鼬模式。这是我所拥有的:

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.cognect.mobile" version="1.0.24" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
<content src="index.html" />
<preference name="DisallowOverscroll" value="true" />
<preference name="phone-gap-version" value="cli-7.1.0" />
<preference name="android-minSdkVersion" value="17" />
    
<plugin name="cordova-plugin-battery-status" source="npm" spec="~1.1.1" />
<and a whole load more plugins...>
    
<platform name="ios">
<icon platform="ios" src="www/res/icon/ios/icon.png" width="1024" height="1024" />                
<icon height="114" platform="ios" src="www/res/icon/ios/icon@2x.png" width="114" />
<icon height="40" platform="ios" src="www/res/icon/ios/icon-40.png" width="40" />
<and a whole load more icons....>        

<splash height="1136" platform="ios" src="www/res/screen/ios/Default-568h@2x~iphone.png" width="640" />        
<splash height="768" platform="ios" src="www/res/screen/ios/Default-Landscape~ipad.png" width="1024" />
<splash height="2048" platform="ios" src="www/res/screen/ios/Default-Portrait@2x~ipad.png" width="1536" />
<splash height="1024" platform="ios" src="www/res/screen/ios/Default-Portrait~ipad.png" width="768" />

</platform>

我得到的回复是const People = { userId: { type: mongoose.Schema.Types.objectId, required: true, ref: 'User' }, locationId: { type: mongoose.Schema.Types.objectId, required: true, ref: 'Location' }, }; const Person = mongoose.model( 'Person', new mongoose.Schema({ ...People, gender: { type: String } }) ); const shmanian = await new Person({gender: 'other', userId:'someUserId', locationId: 'someLocationId'}).save();

问题是当我创建Person时,人们不会被填充。

2 个答案:

答案 0 :(得分:0)

您应该在“人”模型中嵌入“人”数组。例如,

const Schema = require("mongoose").Schema;
const model = require("mongoose").model;

const Person = new Schema({
    userId: {
        type: mongoose.Schema.Types.objectId,
        required: true,
        ref: 'User'
    },
    locationId: {
        type: mongoose.Schema.Types.objectId,
        required: true,
        ref: 'Location'
    },
});

const People = new Schema({
    person: [Person]
});

module.exports = model("People", People);

在这里,每次创建新Person时,都可以将其添加到包含Person对象数组的People模型中。

答案 1 :(得分:0)

不是猫鼬问题。
创建对象PeoplePerson未定义。
因此,它不会被填充。

尝试切换两个分配。