节点 - 无效的 select() 参数。必须是字符串或对象

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

我正在创建一个网站,用户可以在其中编辑已经存在的 post 并将他们自己的图像添加到 post。但是,我在上传任何图像时收到错误消息。我正在使用 Node、Express、Mongoose 和 Cloudinary 来完成这项任务。

这是用户填写的编辑表单:

<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>

这是上传图片和更新 Mongoose 模型的 PUT 路径:

//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("/");
  });
});

这是我在尝试上传图片时收到的错误消息:

(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.

在 "listing" 猫鼬模型中,我有一个名为 "images" 的字符串数组,其中将存储上传的图像 URL。任何帮助将不胜感激。提前致谢。

下面是findByID的参数:

  1. id «Object|String|Number» 要查询的 _id 的值
  2. [projection] «Object|String» 可选字段到 return,参见 Query.prototype.select()
  3. [选项] «对象» 可选参见 Query.prototype.setOptions()
  4. [回调]«函数»

这就是为什么它在传递 upload.array("listing[images]" 时给出 TypeError: Invalid select() argument. Must be string or object. 的原因,它既不是对象也不是字符串。此外,findByID 只会 return 文档,它不会像您尝试的那样更新。您将需要 findByIdAndUpdate 来更新文档。