Geraldo Kandonga Fillipus Geraldo Kandonga Fillipus - 1 month ago 18
Node.js Question

Promise findItem using mongodb and node.js

I am new to mongodb, node.node.js after learning that this platform are good for backend i decided to work on a small project that i want it to run on mongodb as its database

And now i am working on API for my mobile application, but my challenge is i dont know how to read a single Item after creating it,

Is there any help guys

Here is how i am creating An Item

'use strict';

const express = require("express");
const router = express.Router();
const lesson = require('../models/lesson');

exports.getLessons = title =>


//CREATE lessons
export.createNewLesson = (image, title, grade, lessonObjective, subject, chapter, description, username:username)=>{

new Prmise((resolve, reject)=>{
const newLesson = new lesson({
image :image,
title :title,
grade :grade,
lessonObjective :lessonObjective,
subject :subject,
chapter :chapter,
description :description,
created_at : new Date(),

const author = {
id: req.user._id,
username: req.user.username
},

})
newLesson.save();
.then(()=> resolve({ status: 201, message: 'successfully added new lesson'}))
.catch(err =>{
if (err.code == 11000) {
reject({ status: 409, message: 'There is a similar lesson with the same !' });
} else {
reject({ status: 500, message: 'Internal Server Error !' });
}
});
});

// GET all lessons
new Promise((resolve,reject) => {

lesson.find({ title: title },
{ image: 1,
title: 1,
grade: 1,
subject : 1,
chapter: 1,
lessonObjective: 1,
description: 1,
created_at: 1,
_id: 0
})

.then(lessons => resolve(lessons[0]))

.catch(err => reject({ status: 500, message: 'Internal Server Error !' }))

});

// GET lesson by id

//UPDATE lesson
}

Answer

It is hard to help you since your code is messed up.

I've tried to make sense in your code and something like this should work but maybe you will need to make some adjustments regarding to your project.

const Lesson = require('../models/lesson');

export function createNewLesson(image, title, grade, lessonObjective, subject, chapter, description, username) {
  return new Promise((resolve, reject) => {
    let newLesson = new Lesson({
      image: image,
      title: title,
      grade: grade,
      lessonObjective: lessonObjective,
      subject: subject,
      chapter: chapter,
      description: description,
      created_at: new Date(),
      author: {
        id: req.user._id,
        username: req.user.username
      }
    });

    newLesson.save()
      .then(lesson => resolve({ status: 201, message: 'successfully added new lesson' }))
      .catch(err => {
        if (err.code == 11000) {
          reject({ status: 409, message: 'There is a similar lesson with the same!' });
        } else {
          reject({ status: 500, message: 'Internal Server Error!' });
        }
      });
  });
}

export function getLesson(title) {
  return new Promise((resolve, reject) => {
    Lesson.find({ title: title }, {
        image: 1,
        title: 1,
        grade: 1,
        subject: 1,
        chapter: 1,
        lessonObjective: 1,
        description: 1,
        created_at: 1,
        _id: 0
      })
      .then(lessons => resolve(lessons[0]))
      .catch(err => reject({ status: 500, message: 'Internal Server Error!' }))
  });
}


// Usage
getLesson("Coding conventions - Lesson 1")
  .then(lesson => console.log('And there it is:', lesson))
  .catch(error => console.err('Something went wrong.'));

NB: I am assuming you are using Mongoose for your project since it is in your question tags but next time try to be more explicit in your introduction : MongoDB and Mongoose are two different things.