Create a stream of content from the items of users you are following using GraphQL in a NestJS

I've recently started learning about graphql and I have a group of users who can follow each other. Each user also has a list of products. How can I create a feed that displays the products of the users I follow?

The user object includes a property called listing, which is related to the products (which, in turn, are related to the user through the author property)

A typical user object looks like this:

User object
{
  ...
  listing: [ "id1", "id2" ]
}

And a product object looks like this:

Product object
{
  ...
  author: { User1 }
}

What would be the most effective method for querying and retrieving products from followed users? Any additional helpful information:

  • The data is stored in a mongodb database
  • The listings and the author are stored as ids but can be retrieved with the complete object

Thank you!

Answer №1

To tackle the issue, I managed to resolve it with the help of the mongoose find method as shown below:

this.productModel
      .find({
        seller: { $in: following },
      })

In this scenario, following denotes an array containing the user's followed id's. Afterward, I utilized the .populate('seller').exec() function to populate the seller field.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having difficulty creating a TypeScript function

I've encountered a TypeScript error that has left me puzzled: src/helpers.ts:11:14 - error TS2322: There's an issue with this piece of code and I can't quite grasp it: Type '<T extends "horizontal" | "vertical" | undefined, U extends ...

What is the correct way to include a JSON file in TypeScript?

Currently, I'm attempting to reference a JSON path and save it as a constant. My initial approach involved creating a reference with a string and then sending it back as a response using res.send, which successfully returned the string. However, I am ...

Sequelize is encountering an issue where it is unable to read properties of undefined, resulting in a "TypeError: Cannot read properties of undefined (reading 'constructor')"

I encountered a problem with Sequelize and I'm struggling to pinpoint the exact source of the issue. The error message I'm receiving is: TypeError: Cannot read properties of undefined (reading 'constructor') and here is the stack trace: ...

Upon refreshing the page, the Vuex store getter displays [__ob__: Observer]

After each page refresh, my Vuex store getter returns [__ob__: Observer]. The version of Vue being used is 2.2.3 along with TypeScript. Initially, the data loads correctly from an external API, but when utilizing the getter, it fails and requires another ...

The query fails to retrieve results for the specified date and the beginning of the month

I have encountered an issue with my query that is supposed to fetch values between the start and end of the month. Interestingly, when a record is entered on the first day of the month, it doesn't get returned in the query result. Only records entered ...

What is the best way to transfer a property from a nested service to a virtual property declaration in a Schema?

I recently made the decision to transition my existing nodejs + mongoose API to NestJS. As a beginner with this framework, I turned to the official website's documentation for guidance on setting up my configuration services & modules and adapting my ...

The utilization of angular2-materialize is not possible due to an error message indicating that jQuery.easing is undefined

Greetings to all who are taking the time to read this. I am encountering an issue while trying to run ng serve. Here is the error message I am receiving: TypeError: jQuery.easing is undefined Here is a breakdown of what I have done so far: ng new X cd X ...

What's the latest integration technique for AngularSPA and Hapi in 2020?

I've spent countless hours studying articles and digging through documentation with little success. My Angular SPA app works perfectly on localhost, but after deploying it to Heroku, the routing tries some GET requests and HAPI responds with a 404 err ...

Automatically align a Div to the right within an ngFor directive in an Angular 6 application

<div class="row"> <div class="col-lg-4 col-xs-6" *ngFor="let client of clients;index as i"> <!-- small box --> <div class="small-box bg-dashboard-box" > <div class="inner"> <div class="text-black"> ...

Error in TypeScript: Express route handler does not have any overloads that match this call

Encountering a type error while working on an Express.js application with TypeScript. Here's the relevant code snippet: import express from 'express'; import Stripe from 'stripe'; import { config } from '../config'; impor ...

Unexpected error encountered in Angular 2 beta: IE 10 displays 'Potentially unhandled rejection [3] SyntaxError: Expected'

Question regarding Angular 2 Beta: I am starting off with a general overview in the hopes that this issue is already recognized, and I simply overlooked something during my research. Initially, when Angular 2 Beta.0 was released, I managed to run a basic m ...

Use the ngFor directive to iterate over the most recently created array from the parent ng

I am looking to link material tabs with ngFor to generate arrays for child ngFor. Let's start from the beginning: <mat-tab-group> <mat-tab *ngFor="let tab of asyncTabs "> <ng-template mat-tab-label>{{tab.label}}</ng-template ...

The navigation menu items remain static until a manual refresh is performed

I am facing an issue with my Angular app's authentication system. The navmenu displays login, logout, and the username when a user is logged in. However, after logging in, the navbar does not update until the page is refreshed. I can't figure out ...

Tips for effectively extending a styled component with TypeScript

The latest version of styled-components, v4, has deprecated the use of .extend for extending or composing components. The recommended way to do this is shown below: const ButtonA = styled('button')`color: ${props => props.color};` const Butto ...

Using ngIf for various types of keys within a JavaScript array

concerts: [ { key: "field_concerts_time", lbl: "Date" }, { key: ["field_concert_fromtime", "field_concert_totime"], lbl: "Time", concat: "to" }, { key: "field_concerts_agereq", lbl: "Age R ...

The transformation of interfaces into types using module augmentation in Typescript

Can interface be changed to type when adding modules with TS? I am looking to customize the Theme in Material-UI with my own properties. I have created a file called createPalette.d.ts: import '@material-ui/core/styles/createPalette'; declare mo ...

"Access to the DOMRouterOpts interface is restricted and cannot be

Encountering a problem with the import type issue while using react-router-dom:6.22.3 alongside TypeScript in a React project. import { Route, createBrowserRouter, createRoutesFromElements } from "react-router-dom"; import type { RouteObject } fr ...

Angular: Check the validity of a date entered by the user depending on the value selected in another date input

I am working on a form with two input fields: InvoiceDate and InvoiceDueDate. My goal is to ensure that the InvoiceDueDate is set at least 30 days after the selected InvoiceDate. Currently, I have implemented validation for the InvoiceDueDate by setting [ ...

Determining the Best Option: React JS vs Angular UI Framework

After researching the latest versions of React and Angular online, I discovered that both are suitable for developing Web Application UI. However, I also realized that there are key differences to consider when choosing between the two. Let's say I h ...

Vuejs fails to properly transmit data

When I change the image in an image field, the new image data appears correctly before sending it to the back-end. However, after sending the data, the values are empty! Code Commented save_changes() { /* eslint-disable */ if (!this.validateForm) ...