2016-10-25 10 views
0

私はmongodb、node.node.jsの初心者です。このプラットフォームはバックエンドに適していることを学んだ後、私はmongodbをデータベースとして実行したい小さなプロジェクトに取り組みました。どのように私mongodbとnode.jsを使ってfindItemを約束する

そして今、私は自分のモバイルアプリケーションのためのAPIに取り組んでいますが、私の挑戦は、私はそれを作成した後に、単一の項目を読み取る方法を知らないで、

は、任意のヘルプみんな

がここにありますですアイテムを作成しています

'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 
} 
+1

SOの質問をする前に、コードを適切にフォーマットし、構文エラーを修正してください( 'username:username'をパラメータとして定義するか、' new Prmise'を呼び出すなど)。 – nem035

答えて

0

あなたのコードが台無しになってからあなたを助けるのは難しいです。

私はあなたのコードで意味を成就しようとしましたが、これはうまくいくはずですが、あなたのプロジェクトに関していくつかの調整が必要になるでしょう。

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:それはあなたの質問のタグであるので、私はあなたのプロジェクトのためにマングースを使用していると仮定していますが、次回はあなたの導入でより明確にしよう:MongoDBのとマングースは、二つの異なるものです。

+0

感謝、あなたの答えが働いた良い事柄 –

関連する問題