Ticdoc
Ticdoc

Reputation: 268

Meteor Upsert Syntax with Nested Values

I am having trouble trying to get a Collection.upsert to work in Meteor 1.4.3.2.

My app pulls in active listings from eBay and inserts them into the database if they don't already exist, otherwise it updates the listing that is already stored. So, what I am trying is the following in a meteor method:

let upsertObj = {$set: {}};

const account = ListingAccounts.findOne( ... );

if (!account) // throw an error because account info is required by schema

upsertObj.$set['account.id'] = account._id;
upsertObj.$set['account.username'] = account.username;
upsertObj.$set['account.nickname'] = account.nickname;

// ... also gets other listing data such as listingId, title, condition, etc...

return Listings.upsert({listingId: eBayListingID}, upsertObj);

There are other values that are nested similarly to the account details above, and they all seem to work. I've logged the final upsertObj object and the values are valid and comply with my schema (SimpleSchema), but just for good measure, here is an excerpt of the final upsert object I am logging on the server just before the upsert happens:

{ '$set': 
    { 'account.id': 'trustmethisisvalidstring',
      'account.username': 'ValidAccountNameString',
      'account.nickname': 'ValidAccountNicknameString',
      /* more name:values below this */
    }
}

Here is an excerpt from my schema (aldeed:simple-schema 1.5.3)

ListingsSchema = new SimpleSchema({
    account: {
        type: Object
    },
    "account.id": {
        type: String,
        optional: true // added after question asked
    },
    "account.username": {
        type: String,
        optional: true // added after question asked
    },
    "account.nickname": {
        type: String,
        optional: true // was always optional
    },
    ...
});

I am receiving the error on the client with the following details: [{"name":"account.id","type":"required","value":null},{"name":"account.username","type":"required","value":null}]

with the reason reason: "Id is required"

This is the first time I've tried using upsert and I can't help but feel I am missing the mark on something. Maybe my syntax is off, or maybe I'm just not using bracket notation correctly? I don't know, the Meteor docs unfortunately do not have any examples that I could find.

Any assistance or clarification on using Upsert would be super appreciated, thank you!

Upvotes: 1

Views: 78

Answers (0)

Related Questions