- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要创建一个 shopify 订单数据库,以便我可以运行在 shopify 管理区域中无法执行的高级查询和销售报告。我正在构建 Sails .12 和 mysql。 Shopify 允许您注册一个 webhook,这样每次下订单时,它都会创建一个 POST 到指定的 URL,正文中的订单数据为 JSON。订购的产品是一个 JSON 对象数组,作为 POST 中的值之一:
{
"id": 123456,
"email": "jon@doe.ca",
"created_at": "2017-01-10T14:26:25-05:00",
...//many more entires
"line_items": [
{
"id": 24361829895,
"variant_id": 12345,
"title": "T-Shirt",
"quantity": 1,
"price": "140.00",
},
{
"id": 44361829895,
"variant_id": 42345,
"title": "Hat",
"quantity": 1,
"price": "40.00",
},
]
}
我需要将订单保存到一个 Orders 表中,并将订购的产品保存到一个一对多关系的 line_items 表中;一个订单可以有多个 line_items(订购的产品)。 Webhook 发送了 100 多个键值对,我正在保存所有这些。我已经在定义数据类型的地方创建了两个模型,所以现在我有很长的 Order.js 和 Line_item.js 文件,并且我正在使用
line_items: {
collection: 'line_item',
via: 'order_id'
},
在我的 Order.js 中,和
order_id: {
model: 'order'
},
在我的 Line_item.js 模型中关联它们。这是定义我的两个表的正确方法吗?另外,我应该把将 JSON 映射到模型参数的代码放在哪里?如果我将该代码放入 Controller 中,我是否必须再输入 100 多行代码才能将每个 json 值映射到其正确的参数。我将如何保存到两个不同的模型/表?例如:
var newOrder = {};
newOrder.id =req.param('id');
newOrder.email = req.param('email');
newOrder.name = req.param('name');
...//over 100 lines more, then Order.create(newOrder, ...)
var newLine_items = req.params('line_items'); //an array
_.forEach(newLine_items, function(line_item){
var newLine_item = {};
newLine_item.id = line_item.id;
newLine_item.order_id = newOrder.id;
newLine_item.title = line_item.title;
//etc for over 20 more lines, then Line_item.create(newLine_item, ...)
});
最佳答案
我需要将订单保存到一个 Orders 表中,并将订购的产品保存到一个一对多关系的 line_items 表中;一个订单可以有多个 line_items(订购的产品)。
这听起来完全合理,嗯,除了使用牛津逗号 :)
webhook 发送了 100 多个键值对
我不确定我是否完全理解这是什么或它在此过程中的用途。
也就是说,在您的模型中为此设置一个具有 JSON 值的属性可能会有所帮助,然后检索它并将其作为 JSON 使用,而不是尝试手动说明每个属性(如果您是这样的话)那边干什么?
这实际上取决于您的用例以及您将如何使用数据,但我认为如果格式发生变化您可能会遇到问题,如果它只是作为 JSON 对象存储和解析则不是这样?
另外,我应该把将 JSON 映射到模型参数的代码放在哪里
在 v0.12.x 中查看 Services .
在 v1 中,服务仍然可以工作,但将此逻辑移至 Helpers可能是一个不错的选择,但似乎 custom model method会更好。
这是您的代码的较短版本:
var newOrder = req.allParams();
newLine_items = {};
_.forEach(newOrder.line_items, function(line_item) {
newLine_items.push(line_item);
});
您的逻辑可能如下所示:
var newOrder = req.allParams();
// Store the order
Order
.create(newOrders)
.exec(function (err, result) {
if (err) // handle the error
var newLine_items = {};
_.forEach(newOrder.line_items, function(line_item) {
// Add the order id for association
line_item.order_id = result.id;
// Add the new line item with the orders id
newLine_items.push(line_item);
});
// Store the orders line items
LineItems
.create(newLine_items)
.exec(function (err, result) {
if (err) // handle the error
// Handle success
});
});
Order 模型中的生命周期回调:
beforeCreate: function (values, cb) {
delete(values.line_items);
cb();
}
但是你真的应该研究一下 bluebird promises,因为第一版 sails 中的模型方法已经选择支持它们,它有助于否定在我的示例中开始的厄运金字塔,这也是你想要避免的事情:P
关于mysql - Sailsjs MVC 将参数从外部 API 映射到多个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41601276/
在 sailsjs 1.0@beta 中,我有一个模型链接到带有 mysql-sails@beta 适配器的 mysql 表中的 View 。 模型配置如下: attributes: { id
我有一个独立的操作,我使用 CloudSDK 通过单击按钮从我的前端调用它,我得到了我的值,但该操作不会重定向到 View 。我已在成功退出中将 responseType 指定为 View ,但这似乎
sails.sockets.rooms()和sails.sockets.socketRooms()均已弃用 在文档中: This method is deprecated. Please keep t
我尝试使用以下方法在 Controller 操作中获取发布数据: req.body.name 但是不起作用 最佳答案 您可以尝试先检索全部内容来访问它们 function(req, res, next
我一直在尝试按照文档和 StackOverflow 上的说明为我的应用程序加载自定义配置,但失败了。 我在中创建了一个新文件 /config/application.js 内容: module.exp
我对两个 Nodejs 和 sails 都很陌生。我正在尝试在一个 Action 中创建两个模型。我的问题是如何处理两个查询中可能发生的错误。 当前代码: new: function (req, re
所以我的问题是我想将任何请求记录到我的 sails 应用程序。基本上我已经解决了一个问题,即我无法记录请求的参数。 myRequestLogger: function(req, res, next)
是否可以动态更新 sails.config 对象,而无需重新升起 sails 来应用这些更改? 我希望动态更新sails.config.policies 在我的 bootstrap.js 中我已经设置
假设我有一个很长的任务,当有人连接到 InitializeDB 时,该任务就开始运行。 (当然,将来会获得授权,但暂时不考虑)。 'post /initializeDB':'OrderControll
问题 所以我正在编辑一个 CRUD 应用程序以在删除之前进行确认,并创建了一个 JavaScript 函数来执行此操作,但我在 cmd 和“显示”页面上收到以下错误 error: Sending 50
我了解到放置在资源中的静态文件将可以直接在浏览器中访问。但是执行此操作后,我的静态文件无法访问。 最后,结果发现静态文件没有编译到隐藏文件夹(.tmp/public)中。我手动将所有文件复制到这个隐藏
sails.js 框架不支持事务。在 GitHub 问题中,他们谈论 transaction()。但是不适合在一个事务中编写多个查询。 所以我认为一种解决方案是使用 node-mysql 模块。但我认
好吧,我(天真地)试图让 bull 在 sails 应用程序中工作:最终我希望有一个队列,我可以根据传入路线向其中添加/删除/检查任务。 现在,据我所知,要创建一个可在全局范围内运行的排队系统,我必须
我正在开发一个 Sails 应用程序,对于我的单元测试,我需要在 ./test/bootstrap.test.js 和 ./test/unit/controllers/*.test.js 中使用一些变
我一直在努力更好地了解如何在 SailsJS 中设置外键。我目前正在开展一个类(class)项目,我的小组需要创建一个评估系统,其中包含教师和学生的个人资料以查看结果。我在网上看到了一些例子,但我看到
我想创建一条动态路线。我创建了一个名为用户的 API。我需要可以访问/users/:nameofuser,就像自动创建的用户/id。 如何在这种类型的 url 中获取用户名,并执行 Controlle
我有一个模型帖子: // Post.js attributes:{ name: {type:"string"}, users: { collection: 'user', via: '
我的 Sailsjs 应用程序中有以下具有多对多关系的模型: 事件.js: attributes: { title : { type: 'string', required: true }, de
我正在使用 Sails v0.10.5。 我创建了三个模型,它们之间存在关联。存在一个Candidate 模型、一个Evaluator 模型和一个Rating 模型,评估者通过该模型对候选人进行评分。
我的 Sails 应用程序中有用户模型,当然它有密码。因此,当我创建新用户时,Sails 会使用包含新创建用户的所有数据(包括哈希密码)的 json 进行响应。有没有办法阻止 Sails 输出某些字段
我是一名优秀的程序员,十分优秀!