我用Node.js制作API服务器
此外,我还使用sequelize.js(版本4)与MySQL进行通信。
我的桌子结构在这里。
文章
评论
index.controller.js
import { Article, Comment } from '../model/model';
export const index = (req, res) => {
res.send('controller index');
};
export const getArticle = (req, res) => {
try {
Article.all()
.then(article => {
res.status(200).json({status: true, result: article});
});
} catch(e) {
res.status(500).json({status: false, result: "get article fail"});
}
}
export const addArticle = (req, res) => {
const { subject, content } = req.body;
try {
Article.create({
subject: subject,
content: content
})
res.status(200).json({status: true, result: "article write success"});
} catch(e) {
res.status(500).json({status: false, result: "article fail"});
}
}
export const getComment = (req, res) => {
try {
Comment.all()
.then(comment => {
res.status(200).json({status: true, result: comment})
});
} catch(e) {
res.status(500).json({status: false, result: "get comment fail"});
}
}
export const addComment = (req, res) => {
const { content, article_no } = req.body;
try {
Comment.create({
content: content,
article_no: article_no
})
.then(() => res.status(200).json({status: true, result: "comment write success"}))
} catch(e) {
console.log(e);
res.status(500).json({status: false, result: "comment fail"});
}
}index.js
import express from 'express';
import { index, getArticle, getComment,addArticle, addComment } from './index.controller';
const router = express.Router();
router.get('/', index);
router.get('/article', getArticle);
router.post('/article', addArticle);
router.get('/comment', getComment);
router.post('/comment', addComment);
export default router;model.js
import Sequelize from 'sequelize';
const sequelize = new Sequelize('db', 'id', 'pw', {
host: '127.0.0.1',
dialect: 'mysql'
})
export const Article = sequelize.define('article', {
no: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
subject: {
type: Sequelize.STRING,
allowNull: false
},
content: {
type: Sequelize.STRING,
allowNull: false
}
}, {
freezeTableName: true,
underscored: true
})
export const Comment = sequelize.define('comment', {
no: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
content: {
type: Sequelize.STRING,
allowNull: false
}
}, {
freezeTableName: true,
underscored: true
})
Article.hasMany(Comment, {as: 'Comments'}); // association
Comment.belongsTo(Article); // association
sequelize.sync({
force: false
});由于关联(hasMany,belongsTo),article_no列将被添加到注释表中。
参考本文档,http://docs.sequelizejs.com/manual/tutorial/associations.html#one-to-many-associations-hasmany-
上面说Instances of Project will get the accessors getWorkers and setWorkers.
在我的例子中,它将是getComments和setComments。
但我不知道如何才能得到与使用访问器有关的所有评论文章。
目前的输出在这里。(如果我连接到GET /article)
{
"status":true,
"result":[
{
"no":1,
"content":"comment test",
"created_at":"2018-07-18T05:00:45.000Z",
"updated_at":"2018-07-18T05:00:45.000Z",
"article_no":1
}
]
}期望的输出在这里
{
"status":true,
"result":[
{
"no":1,
"content":"comment test",
"created_at":"2018-07-18T05:00:45.000Z",
"updated_at":"2018-07-18T05:00:45.000Z",
"article_no":1,
"comments": [
// related comments here!
]
}
]
}谢谢。
发布于 2018-07-18 05:59:04
当您想加入另一个模型时,应该在查询中使用include。
User.findAll({
include: [
{ model: Profile, required: true // inner join }
],
limit: 3
});查看后缀模型使用文档。
要使用访问器访问注释,您需要执行如下操作:
const articles = await Article.all();
articles.forEach(article => {
const comments = await article.getComments();
})背后的想法是,每个article sequelize对象都有访问器getComments,但是在内部,当您执行getComments时,它会向数据库发出一个新的请求,并在注释where查询中使用预先填充的articleId。这称为延迟加载,因为您可以在需要时加载数据。但那不是你的案子。
对于所需的输出,我建议使用include方法,因为它将向数据库发出单个请求。
https://stackoverflow.com/questions/51394054
复制相似问题