javascript - Sailsjs findOrCreate 使用什么属性来查找记录是否存在?

标签 javascript mysql node.js sails.js waterline

我将 sailsjs 与 mysql 结合使用,并在下订单时使用它保存来自 shopify webhook 的数据。它返回一个 json 对象,其中一个属性是一个客户对象,我将其保存在与主订单表相关的另一个表中。我在客户模型上使用 findOrCreate,所以我没有重复的客户。

水线能否使用特定属性来确定记录是否已经存在?客户对象(来自 shopify)有一个 id,但文档没有明确说明不同订单中的给定客户是否相同。此外,如果客户设置不同的付款方式、账单地址等,waterline 是否会为其创建新记录?

综上所述,我需要 waterline 来使用 customer_email 属性来确定记录是否存在。这可能吗?对不起,乱七八糟;我一边打字一边思考这个问题。

最佳答案

findOrCreate接受 2 个参数和一个回调 您应该首先使用唯一标识键来确定记录是否存在。

例如

var findCriteria = { id: 1, customer_email: 'abc@example.com' };
var recordToCreate = { id: 1, customer_email: 'abc@example.com', name: 'ABC' };
Model.findOrCreate(findCriteria, recordToCreate, console.log);

findOrCreate

Checks for the existence of the record in the first parameter. If it can't be found, the record in the second parameter is created. If no parameters are passed, it will return the first record that exists. If no record is provided, it will either find a record with matching criteria or create the record if the object can not be found. Eg. Model.findOrCreate( findCriteria , recordToCreate , [callback] )

关于javascript - Sailsjs findOrCreate 使用什么属性来查找记录是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41727322/

相关文章:

Mysql:如何使表在子查询中可见?

mysql - 如何从 Joomla 批量删除用户?我的网站遭到垃圾邮件机器人攻击,我有超过 40,000 名注册用户,但只有 1 名活跃用户。

javascript - 使用 socket node.js 检测用户切换页面

node.js - 使用imagemin压缩图片并存储在mongodb中

javascript - 通过 webapp2 重定向时 Google Cloud Endpoints 未加载

mysql - 如何从现有数据库导入到 opencart 数据库

javascript - 如何断言一个函数是从另一个函数中调用的?

node.js - 在 NodeJS 中使用流进行 TDD/测试

PHP 代码中的 JavaScript 内联验证

javascript - $_POST 无法从 html 文件中的文本字段检索数据