اذهب إلى المحتوى
  • 0

إظهار البيانات الخاصة بالمستخدم

محمود سعداوي2

السؤال

السلام عليكم.

قمت بالكود التالي قصد إظهار البيانات الخاصة بالمستخدم (الإسم و الصورة) لكل post.

// Get All Images
const allImages = async (req, res) => {
  try {
    let posts = await Image.find()
      .sort({ date: -1 })
      .populate("user", "name avatar id");
    res.json(posts);
  } catch (error) {
    console.log(error.message);
    res.status(500).send("Server error");
  }
};

Image Model

const ImageSchema = new Schema({
  user: {
    type: Schema.Types.ObjectId
  },
  title: {
    type: String,
    required: true
  },
  description: {
    type: String,
    required: true
  },
  photo: {
    type: Object,
    default: {
      url: "",
      publicId: null,
    },
  },
  likes: [
    {
      user: {
        type: Schema.Types.ObjectId
      }
    }
  ],
  comments: [
    {
      user: {
        type: Schema.Types.ObjectId
      },
      text: {
        type: String
      },
      date: {
        type: Date,
        default: Date.now
      }
    }
  ],
  date: {
    type: Date,
    default: Date.now
  }
});

المشكل هو ظهور الid الخاصة بالمستخدم دون ظهور بقية البيانات

Capture.JPG.4ad32f9c72127213e70de3b55370c112.JPG

شكرا على المساعدة

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

عند تعريف ال user يجب ان تقوم بإضافة الـ reference حتى تعرف mongoose ماهي الـ collection التي تقصدها :

const ImageSchema = new Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: "example" // اسم المودل التي تحتوي المستخدمين
  },
  title: {
    type: String,
    required: true
  },
...

وهذا مثال بسيط يوضح كيفية ربط المودل ببعضهم البعض :

const mongoose = require('mongoose');
const { Schema } = mongoose;

const personSchema = Schema({
  name: String,
  age: Number,
  stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

const storySchema = Schema({
  author: { type: Schema.Types.ObjectId, ref: 'Person' },
  title: String,
  fans: [{ type: Schema.Types.ObjectId, ref: 'Person' }]
});

const Story = mongoose.model('Story', storySchema);
const Person = mongoose.model('Person', personSchema);

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...