Jordan Pisani
Jordan Pisani

Reputation: 107

Node - Invalid select() argument. Must be string or object

I am creating a website where a user can edit an already existing post and add their own images to the post. I receive an error when I upload any images, however. I am using Node, Express, Mongoose, and Cloudinary to accomplish this task.

Here is the edit form users fill out:

<div class="row">
    <h1 style="text-align: center">Upload photos for: <%= listing.address %></h1>
    <div style="width: 30%; margin: 25px auto;">
        <form action="/<%= listing._id %>/dashboard?_method=PUT" method="POST" enctype="multipart/form-data">
            <div class="form-group">
                <label for="image">Images</label>
                <input type="file" id="images" name="listing[images]" accept="image/*" multiple required>
            </div>
            <div class="form-group">
                <button class="btn btn-lg btn-primary btn-block">Submit!</button>
            </div>
        </form>
        <a href="/">Go Back</a>
    </div>
</div>

Here is the PUT route to upload the images and update the Mongoose model:

//submit photos
router.put("/:id/dashboard", isLoggedIn, async function(req, res){
  Listing.findById(req.params.id, upload.array("listing[images]"), async function(err, foundListing){
    req.body.listing.images = req.body.listing.images || [];
    for (const file of req.files) {
        let result = await cloudinary.uploader.upload(file.path);
        req.body.listing.images.push(result.secure_url);
    }
    res.redirect("/");
  });
});

And here is the error I receive when I try to upload images:

(node:8048) UnhandledPromiseRejectionWarning: TypeError: Invalid select() argument. Must be string or object.
(node:8048) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:8048) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

In the "listing" mongoose model I have an array of Strings with the name "images" where the uploaded image URLs would be stored. Any help would be greatly appreciated. Thanks in advance.

Upvotes: 0

Views: 1795

Answers (1)

Sookie Singh
Sookie Singh

Reputation: 1623

Below are the params of findByID:

  1. id «Object|String|Number» value of _id to query by
  2. [projection] «Object|String» optional fields to return, see Query.prototype.select()
  3. [options] «Object» optional see Query.prototype.setOptions()
  4. [callback] «Function»

That's why it is giving TypeError: Invalid select() argument. Must be string or object. as you are passing upload.array("listing[images]" which is neither an object nor a string. Also, findByID will just return the document and it won't update as you are trying to do. You will need findByIdAndUpdate to update a document.

Upvotes: 1

Related Questions