【问题标题】:Promise findItem using mongodb and node.js使用 mongodb 和 node.js 承诺 findItem
【发布时间】:2017-03-07 15:28:30
【问题描述】:

我是 mongodb 的新手,node.node.js 在得知这个平台适合后端后,我决定做一个小项目,我希望它在 mongodb 上作为其数据库运行

现在我正在为我的移动应用程序开发 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
}

【问题讨论】:

  • 在提出关于 SO 的问题之前,请正确格式化您的代码并修复语法错误(例如将username:username 定义为参数或调用new Prmise)。

标签: node.js mongodb mongoose promise


【解决方案1】:

由于您的代码混乱,很难帮助您。

我已经尝试让您的代码有意义,这样的事情应该可以工作,但也许您需要对您的项目进行一些调整。

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.'));

注意:我假设您正在为您的项目使用 Mongoose,因为它在您的问题标签中,但下次尝试在您的介绍中更加明确:MongoDB 和 Mongoose 是两个不同的东西。

【讨论】:

    猜你喜欢
    • 2015-04-08
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 2018-04-05
    • 2017-05-17
    • 2018-08-02
    • 2017-12-20
    相关资源
    最近更新 更多