【发布时间】:2018-10-21 03:18:08
【问题描述】:
这似乎是一件非常基本的事情,但我真的找不到正确的方法。我的 Node/Express/Sequelize 项目本质上将保存单个用户搜索(到 Searches 表)和来自搜索结果的一个或多个链接(到 Results 表)。用户搜索、获取结果、选择他们认为有用的结果,然后点击保存按钮以保存搜索查询和所选结果。
一个搜索有很多结果,所以结果对象会有一个外键 searchId(这让我认为需要先保存搜索对象才能在每个结果对象上放置一个 id)。到目前为止,我可以使用以下代码成功保存一个搜索项:
controllers/search.js
const Search = require('../models').Search;
module.exports = {
create(req, res) {
return Search
.create({
text: req.body.search_text
})
.then(search => res.status(201).send(search))
.catch(error => res.status(400).send(error));
} };
routes/index.js
const searchController = require('../controllers').search;
const resultController = require('../controllers').result;
const axios = require('axios');
const bingWebSearch = require('../helpers/bingWebSearh')
module.exports = (app) => {
//some app.get requests here;
app.post('/search/results', searchController.create);
};
那个app.post 应该将搜索保存到搜索表中,并将(可能)多个结果保存到结果表中,并将搜索对象 id 作为关联。我应该如何处理这个?它应该在一个 app.post 请求中,还是应该分成两个,第一个保存搜索,然后以某种方式转到第二个 app.post 请求来处理结果(并且还从新创建的搜索中传递 id目的)?
【问题讨论】:
标签: node.js express post sequelize.js