Search code examples
node.jsmongodbtypescriptmongooseaggregation-framework

Mongoose TypeScript Aggregation: does not exist on type 'any[]'


I'm trying to swap out a regular mongo call for an aggregate call. the original that was working is ..

const account = await userModel
     .findOne({ 'shared.username': username })
     .exec();
console.log(account._id)

The new aggregate function is...

      const account = await userModel.aggregate([
        {
            $match: {
                'shared.username': username,
            },
        },
        {
            $lookup: {
                as: 'photos',
                foreignField: 'userId',
                from: 'photos',
                localField: '_id',
            },
        },
    ]);
     console.log(account._id)
//                       ^ Error

The error im getting is

Property '_id' does not exist on type 'any[]'.ts(2339)

im sure their is _id being returned as i can see it in Studio 3T aggregation output

{ 
    "_id" : ObjectId("5e10983cb182628af48e590a"), 
    "shared" : {
        "currency" : "USD", 
        "followers" : 0, 
        "following" : 0, 
        "language" : "en", 
        "loggedIn" : true, 
        "twofactor" : false, 
        "warningMessage" : "verify", 
        "email" : "[email protected]", 
        "fullName" : "James", 
        "username" : "57fe31142e10", 
        "location" : "/57fe31142e10"
    }, 
    "photos" : [
        {
            "_id" : ObjectId("5e117b8f227a32597cdcbb6e"), 
            "category" : "Double Deckers", 
            "previewId" : "5e10983cb182628af48e590a/0f5d0010a4e55794419c03328184cfb45984bf43.jpg", 
            "published" : true, 
            "thumbnailId" : "5e10983cb182628af48e590a/54265d07a962f8544a9ebdc1d876775d9eeed471.jpg", 
            "userId" : ObjectId("5e10983cb182628af48e590a"), 
            "zoomId" : "5e10983cb182628af48e590a/fe0b2016a0be2b26259aaf5152ef22edfffa0c57.jpg", 
            "createdAt" : ISODate("2020-01-05T06:00:47.756+0000"), 
            "updatedAt" : ISODate("2020-01-05T06:00:47.756+0000"), 
            "__v" : 0
        }, 
        {
            "_id" : ObjectId("5e11ab2f0f451779f70f89b1"), 
            "category" : "Single Decker", 
            "previewId" : "5e10983cb182628af48e590a/30d496e44faae1345e4c555accfdc6446fd11945.jpg", 
            "published" : true, 
            "thumbnailId" : "5e10983cb182628af48e590a/9293dd5517f694341a2e582df670dc9bbaba0763.jpg", 
            "userId" : ObjectId("5e10983cb182628af48e590a"), 
            "zoomId" : "5e10983cb182628af48e590a/0a038434e857b05ef8aa46da6dab01259ba2b03e.jpg", 
            "createdAt" : ISODate("2020-01-05T09:23:59.007+0000"), 
            "updatedAt" : ISODate("2020-01-05T09:23:59.007+0000"), 
            "__v" : 0
        }, 
        {
            "_id" : ObjectId("5e11aba00f451779f70f89b2"), 
            "category" : "Midi", 
            "previewId" : "5e10983cb182628af48e590a/2267d14aa642d6cee86319909177ce4eef45cbcc.jpg", 
            "published" : true, 
            "thumbnailId" : "5e10983cb182628af48e590a/9d006f1ed361540ecf9d24c807a111770cf9e44f.jpg", 
            "userId" : ObjectId("5e10983cb182628af48e590a"), 
            "zoomId" : "5e10983cb182628af48e590a/88a87440a9b7845aa44c72411edddc98ea56f6b9.jpg", 
            "createdAt" : ISODate("2020-01-05T09:25:52.019+0000"), 
            "updatedAt" : ISODate("2020-01-05T09:25:52.019+0000"), 
            "__v" : 0
        }
    ]
}

Solution

  • The error is correct, aggregation output is an Array of objects.

    You want to do: (assuming the aggregation returns results)

    console.log(account[0]._id) 
    

    Make sure you resolve the promise by using await/then on the db call otherwise you would be in the loop and it will cause the mongo client loop error