【发布时间】: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