gpt4 book ai didi

node.js - 插入嵌套对象不保存所有数据

转载 作者:太空宇宙 更新时间:2023-11-03 22:05:03 25 4
gpt4 key购买 nike

我正在尝试插入带有嵌套对象的记录。我的目的是存储一个在主要事件下可以有许多链接帖子或图像帖子的事件。当我尝试保存时,顶级数据被保存,但嵌套对象仅显示“链接:[],图像:[]”,而它们每个都应该存储三个值。

var eventSystem = require ("mongoose");
eventSystem.connect('mongodb://localhost/historydb', { useNewUrlParser: true });
var linkPostSchema= new eventSystem.Schema({
linkurl: String,
title: String,
story: String
});
var linkPost = eventSystem.model("linkPost", linkPostSchema);
var imagePostSchema= new eventSystem.Schema({
src: String,
title: String,
story: String
});
var imagePost = eventSystem.model("imagePost", imagePostSchema);
var postSchema= new eventSystem.Schema({
link: [linkPostSchema],
image: [imagePostSchema]
});
var Post = eventSystem.model("Post", postSchema);
var eventSchema = new eventSystem.Schema({
name: String,
date: Date,
story: String,
posts: [postSchema]
});

var Event = eventSystem.model("Event", eventSchema);
var newEvent = new Event({
name: "MAIN EVENT",
date: Date.now(),
story: "main event story"
});

newEvent.posts.push(
{
imagePost: {
src: "first.jpg",
title: "image post title",
story: "image post story"
},
linkPost: {
linkurl: "https://youtube.com",
title: "link post title",
story: "link post story"
}
});

实际结果:

{ _id: 5cf0aea863a51b129a61288f,
name: 'MAIN EVENT',
date: 2019-05-31T04:33:44.117Z,
story: 'main event story',
posts: [ { _id: 5cf0aea863a51b129a612890, link: [], image: [] } ],
__v: 0
}

预期结果:

{ _id: 5cf0aea863a51b129a61288f,
name: 'MAIN EVENT',
date: 2019-05-31T04:33:44.117Z,
story: 'main event story',
posts: [ {
_id: 5cf0aea863a51b129a612890,
link: [linkPost: {
linkurl: "https://youtube.com",
title: "link post title",
story: "link post story"
}], image: [imagePost: {
src: "first.jpg",
title: "image post title",
story: "image post story"
}] } ],
__v: 0
}

最佳答案

我明白了!正确的语法应该是:

newEvent.posts.push({
image: [{
src: "first.jpg",
title: "image post title",
story: "image post story"
}],
link: [{
linkurl: "https://youtube.com",
title: "link post title",
story: "link post story"
}]
});

关于node.js - 插入嵌套对象不保存所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56388946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com