【发布时间】:2020-11-12 18:17:36
【问题描述】:
我正在尝试访问存储在我的对话模型中的消息模型数组。但是,当我使用 populate 方法尝试将 Message 模型存储为数组时,只会显示第一个 Message。
socket.on('connected', function (data) {
//load all messages
const filter = { roomId: data.roomid };
(async () => {
console.log('searching for Schema');
let conversation = await Conversation.findOne(filter)
.populate('messages')
.exec(function (err, message) {
if (err) console.log('no schema found');
var array = message.messages;
console.log(array);
// printing only first Message
});
})();
});
对话模式
const ConversationSchema = new mongoose.Schema(
{
roomId: {
type: String,
required: true
},
messages: {
type: mongoose.Schema.Types.ObjectId, ref: 'Message'
}
},
{
timestamps: true
}
);
【问题讨论】:
-
您是要存储更新消息还是检索消息?
-
我正在尝试检索消息
-
将您的消息字段更新为
messages: [{type: mongoose.Schema.Types.ObjectId, ref: 'Message' }]并确保您在此字段中有数据
标签: javascript node.js reactjs mongodb mongoose