使用Node.js Express的Rest API

什里雅斯黑达

我正在使用登录api。

后端:Node.JS数据存储区:MongoDB

我正进入(状态 TypeError

app.js

const express = require('express');
const app = express();
const bodyParser = require('body-parser');

app.use(bodyParser.json());

var db = require('./db')
db.connect(() => {
  app.listen(3000, function (){
      console.log(`Listening`);
  });
});


const loginRoute = require('./routes/login');
app.use('/login', loginRoute);

路由器- login.js

const express = require('express');
const router = express.Router();
const loginSchema = require('../models/LoginSchema');
const db = require('../db');

router.get('/', (req, res) => {
    db.collection('login').find({}, function (err, result) {
        console.log(result);
        res.send('We are at login api'+result);
    });
});

router.post('/', (req, res) => {
    const logindata = new loginSchema({
        name: req.body.name,
        email: req.body.email,
        password: req.body.password
    });
    db.collection('login').insert(logindata, function(err, data) {
        res.json(data);
    });
});

module.exports = router;

架构: loginschema.js

const mongoose = require('mongoose');

const loginschema = mongoose.Schema({
    name: {
        type : String
    },
    email: {
        type : String,
        required : true
    },
    password: {
        type: String,
        require: true
    }
});

module.exports = mongoose.model('login', loginschema);

MongoDB连接: db.js

const mongoClient = require('mongodb').MongoClient;
const mongoDbUrl = 'mongodb://127.0.0.1:27017';
let db;

function connect(callback){
    mongoClient.connect(mongoDbUrl, { useUnifiedTopology: true }, (err, client) => {
        db = client.db('test');
        callback();
    });
}
function get(){
    return db;
}

function close(){
  db.close();
}

module.exports = {
    connect,
    get,
    close
};

这是我得到的错误:

TypeError: db.collection is not a function  
    at C:\rest-api\routes\login.js:19:8  
    at Layer.handle [as handle_request] (C:\rest-api\node_modules\express\lib\router\layer.js:95:5)  
    at next (C:\rest-api\node_modules\express\lib\router\route.js:137:13)  
    at Route.dispatch (C:\rest-api\node_modules\express\lib\router\route.js:112:3)  
    at Layer.handle [as handle_request] (C:\rest-api\node_modules\express\lib\router\layer.js:95:5)  
    at C:\rest-api\node_modules\express\lib\router\index.js:281:22  
    at Function.process_params (C:\rest-api\node_modules\express\lib\router\index.js:335:12)  
    at next (C:\rest-api\node_modules\express\lib\router\index.js:275:10)  
    at Function.handle (C:\rest-api\node_modules\express\lib\router\index.js:174:3)  
    at router (C:\rest-api\node_modules\express\lib\router\index.js:47:12)  
    at Layer.handle [as handle_request] (C:\rest-api\node_modules\express\lib\router\layer.js:95:5)  
    at trim_prefix (C:\rest-api\node_modules\express\lib\router\index.js:317:13)  
    at C:\rest-api\node_modules\express\lib\router\index.js:284:7  
    at Function.process_params (C:\rest-api\node_modules\express\lib\router\index.js:335:12)  
    at next (C:\rest-api\node_modules\express\lib\router\index.js:275:10)  
    at C:\rest-api\node_modules\body-parser\lib\read.js:130:5  

帮助将不胜感激

帕斯卡尔·拉默斯

直接使用猫鼬模型获取文档

代替

db.collection('login').find({}, function (err, result) {

});
// This will fail since 'collection' is not exported by your db.js

尝试直接使用您的猫鼬模型,例如:

// find all Login Documents 
loginSchema.find({}, function (err, result) {

});
// create a new document
const newlogin = new loginSchema(); 
newlogin.save((err, saveddoc) => {

});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Node.js和Express进行简单的API调用

如何使用node.js实现安全的REST API

如何解析Rest Api json响应并将数据插入Jade模板(Node.js + Express)?

在使用node.js中的REST API时请求参数

Rest API Node Js&Express,创建指向自身的链接(href)

使用Node.js API

简单的Node.JS REST API调用

使用MongoDB与Express api同时运行的Node JS应用

限制Node JS Express中的api访问

(仅限Safari)Axios请求不会将cookie发送到Node.js / Express REST API

如何通过REST API微服务(使用Express构建)在节点js中使用MongoDB更改流

Node和Express中的Rest API,与ejs和React.js一起使用

Express.js查找使用客户端的REST API的URL

使用Firebase Cloud Functions(没有Express)创建Node.js REST API?

在基于Node.js,Express和MongoDb的REST Api中使用绝对引用

REST API node.js

Node.js-使Node.js / Express与Web Audio API一起使用

使用 Node Js 和 express 调用来自 rest api 的 post 请求时出错。请求数据变空

使用 express 和 ES6 类在 node js rest api 中查找路由的问题

Node.js Twilio 使用 REST API 进行调用

使用 node.JS 应用程序错误编写 express JS API

Node.js 和 express Rest api 来创建自定义字段路由

Node JS Express API 事务

Azure 上的 SQL Server 连接到 REST API Express.js/Node.js [TypeError: req.sql is not a function]

Node.js - 使用 supertest 测试 REST API 路由

如何使用 Node.js、Express 和 SQL DB 编写 POST REST API?

如何使用 Node.js、Express、MS SQL 编写 GET REST API 以将数据插入具有多个参数的数据库?

Express:使用子资源定义 REST API?

使用 mongoDB 和 express.js 实现分页 REST API