将多条记录添加到模型's系列帆船

Add multiple records to model's collection Sailsjs

本文关键字:系列 模型 记录 添加      更新时间:2023-09-26

我的Sailsjs应用程序中有以下具有多对多关系的模型:

event.js:

attributes: {
 title : { type: 'string', required: true },
 description : { type: 'string', required: true },
 location : { type: 'string', required: true },
 maxMembers : { type: 'integer', required: true },
 currentMembers : { collection: 'user', via: 'eventsAttending', dominant: true },
 creator : { model: 'user', required: true },
 invitations : { collection: 'invitation', via: 'eventID' },
 tags : { collection: 'tag', via: 'taggedEvents', dominant: true },
 lat : { type: 'float' },
 lon : { type: 'float' },
},

tags.js:

attributes: {
 tagName : { type: 'string', unique: true, required: true },
 taggedEvents : { collection: 'event', via: 'tags' },
},

根据文档,这种关系看起来是正确的。我在tag.js中有以下方法,它接受一个标记字符串数组和一个事件id,并应该添加或删除传入的标记:

modifyTags: function (tags, eventId) {
var tagRecords = [];
_.forEach(tags, function(tag) {
    Tag.findOrCreate({tagName: tag}, {tagName: tag}, function (error, result) {
        tagRecords.push({id: result.id})
    })
})
Event.findOneById(eventId).populate('tags').exec(function(error, event){
    console.log(event)
    var currentTags = event.tags;
    console.log(currentTags)
    delete currentTags.add;
    delete currentTags.remove;
    if (currentTags.length > 0) {
      currentTags = _.pluck(currentTags, 'id');
    }
    var modifiedTags = _.pluck(tagRecords, 'id');
    var tagsToAdd = _.difference(modifiedTags, currentTags);
    var tagsToRemove = _.difference(currentTags, modifiedTags);
    console.log('current', currentTags)
    console.log('remove', tagsToRemove)
    console.log('add', tagsToAdd)
    if (tagsToAdd.length > 0) {
      _.forEach(tagsToAdd, function (tag) {
        event.tags.add(tag);
      })
        event.save(console.log)
    }
    if (tagsToRemove.length > 0) { 
      _.forEach(tagsToRemove, function (tagId) {
         event.tags.remove(tagId)
      })
       event.save()
    }      
})
}

这就是从事件模型调用方法的方式:

afterCreate: function(record, next) {
  Tag.modifyTags(tags, record.id)
  next();
}

当我发布到事件/创建时,我会得到以下结果:http://pastebin.com/PMiqBbfR.

看起来方法调用本身是循环的,而不仅仅是tagsToAdd或tagsToRemove数组。更令人困惑的是,在事件的最后一个日志中,事件似乎有正确的标签。然而,当我发布到event/1时,标记数组是空的。我也尝试过在每次.add()之后立即保存,但仍然得到了类似的结果。

理想情况下,我希望循环遍历tagsToAdd和tagsToRemove数组,在模型的集合中修改它们的id,然后在模型上调用.save()一次。

我已经花了很多时间试图调试这个,所以任何帮助都将不胜感激!

您的实现有一些问题,但主要问题是您将某些方法(即.save().findOrCreate)视为同步方法,而它们(与所有Waterline方法一样)是异步需要回调。因此,您可以有效地并行运行一堆代码,而不是等待它完成后再返回。

此外,由于您想要做的似乎是用这个新列表替换当前事件标记,因此您提出的方法有点过于设计了——您不需要使用event.tags.addevent.tags.remove。您可以使用普通的旧update

因此,您可能会将modifyTags方法重写为:

modifyTags: function (tags, eventId, mainCb) {
  // Asynchronously transform the `tags` array into an array of Tag records
  async.map(tags, function(tag, cb) {
    // For each tag, find or create a new record.
    // Since the async.map `cb` argument expects a function with 
    // the standard (error, result) node signature, this will add
    // the new (or existing) Tag instance to the resulting array.
    // If an error occurs, async.map will exit early and call the
    // "done()" function below
    Tag.findOrCreate({tagName: tag}, {tagName: tag}, cb);
  }, function done (err, tagRecords) {
    if (err) {return mainCb(err);}
    // Update the event with the new tags
    Event.update({id: eventId}, {tags: tagRecords}).exec(mainCb);
  });
}

请在此处查看async.map的完整文档。

如果您想坚持使用.add.remove来实现,那么您仍然希望使用async.map,并在done方法中执行其余的逻辑。您不需要两个.save呼叫;只需先运行所有的.add.remove代码,然后再运行一个.save(mainCb)即可完成

我不知道你想通过从currentTags中删除.add.remove方法来实现什么(这是对event.tags的直接引用),但它不起作用,以后只会引起混乱!