续集协会和api路线。无法从db检索信息

时间:2018-06-06 19:45:53

标签: javascript express server sequelize.js

我真的很新的续集和表达,我已经被困了好几个小时已经试图围绕我想要在我的数据库中的关联以及如何从我的数据库中检索信息。我创建了一个寄宿生板(如冲浪板......和骑手)数据库。我有三个主要模型:Boards,Riders和Makers。 - 制造商有很多板子,板子只能属于制造商。我认为这个协会没有任何问题。 - 骑手可以有很多板子,板子可以属于很多车手。

在填充我的数据库时,我有一个带有riderId和boardsId的RiderBoards表......没问题就到那里......

有时,启动我的服务器时会出现OID错误,但随后它会消失......

现在,在我的api / riders路线中,我正在尝试使用" findAll()"来检索包含所有骑手对象的数组。方法。除了空数组之外,它不会给我任何东西。这是我的数据库或关联的问题吗?我的api路线有问题吗?这是连接到我的数据库的问题吗?我怎样才能找到我想要的东西?我真的很感激这方面的一些指导。

这是我的api / riders路线:

const router = require('express').Router();
const asyncHandler = require('express-async-handler');
const { Rider } = require('../database/index');

router.get(
  '/',
  asyncHandler(async (req, res, next) => {
    const allRiders = await Rider.findAll();
    if(allRiders.length === 0){
      console.log(allRiders);
      res.json('No Riders found')
    }else{
      console.log(allRiders);
      res.json({allRiders});
    }
    /* etc */
  })
);
router.post('/', function (req, res, next) {
  /* etc */
});
router.put('/:puppyId', function (req, res, next) {
  /* etc */
});
router.delete('/:puppyId', function (req, res, next) {
  /* etc */
});

module.exports = router;

这是我的数据库模型的索引:

"use strict";

const db = require("./db");
const Board = require("./board");
const Rider = require("./rider");
const Maker = require("./maker");

// Assosiations go here
Maker.hasMany(Board);
// Rider.hasMany(Board);
Board.belongsTo(Maker);
Board.belongsToMany(Rider,{through: 'RiderBoards'});
Rider.belongsToMany(Board,{through:'RiderBoards'});

module.exports = {
  db,
  Board,
  Rider,
  Maker
};

这是我在服务器端的索引:

const express = require("express");
const session = require("express-session"); // sessions allow us to keep user autheticated even after refreshing their browser window
const app = express();
const morgan = require("morgan");
const path = require("path");
const apiRoutes = require("./api");
const db = require("./database/db");
const passport = require("passport");

const SequelizeStore = require("connect-session-sequelize")(session.Store);
const dbStore = new SequelizeStore({ db: db });

dbStore.sync();

//logging middleware

app.use(morgan("dev"));

//static middleware
app.use(express.static(path.join(__dirname, "../public")));
app.use(express.json());
app.use(express.urlencoded({ extended: true })); // The “extended” syntax allows for rich objects and arrays to be encoded into the URL-encoded format

app.use('/api', apiRoutes);

app.use(
  session({
    secret: process.env.SESSION_SECRET || "Some insecurely saved secret", // the process.env.SESSION_SECRET allows us to use the secret from the enviroment variable instead of exposing our own secret
    resave: false,
    saveUninitialized: false,
    store: dbStore
    // duration: -> how long willthe session be alive for
    // activeDuration: INT -> before logging an user out, it allows INT extra time if require by the user
  })
);

app.use(passport.initialize()); // We initilize passport so that it will consume our req.session object, and attach the user to the request object.
app.use(passport.session());

app.get("*", (req, res, next) => {
  res.sendFile(path.join(__dirname, "../public/index.html"));
});

app.use((req, res, next) => {
  console.log(err);
  console.log(err.stack);
  res.status(err.status || 500).send(err.message || "internal server error.");
});

module.exports = app;

感谢您提供的任何帮助/指导!

1 个答案:

答案 0 :(得分:0)

我终于知道发生了什么:每当我的数据库同步时,我就强迫真正!将其更改为true会将我的记录保存在我的数据库中,因此在我的查询被命中时会有实际记录。