reactjs ObjectId ID不属于函数类型

6yoyoihd  于 2023-02-15  发布在  React
关注(0)|答案(1)|浏览(146)

https://i.stack.imgur.com/7Tq6v.png

import {MongoClient, ObjectId} from "mongodb";
async function handler(req, res){
    if(req.method === "POST"){
        const data = req.body;
        const client = await MongoClient.connect("mongoDB URL")
        const db = client.db();
        const postItCollection = db.collection("postIts");
        const result = await postItCollection.updateOne({_id: ObjectId(data.id)}, {$set:{positionX:data.y, positionY:data.x}});
        console.log(result);
        client.close();
        res.status(201).json({message:"success"})
    }
}
export default handler;
  • 我附上一张图片以防万一。我已经找了三个小时,但我找不到答案....我想地球上没有人有和我一样的问题....
  • 我尝试安装“bson-objectid”库和“mongoose”类型属性
import mongoose from "mongoose";
const { ObjectId } = mongoose.Types;

我试过了,但是我看到同样的错误“方法表达式不是函数类型”
帮帮我,地球上所有的主人。
新手送的。

import {MongoClient, ObjectId} from "mongodb";
async function handler(req, res){
    if(req.method === "POST"){
        const data = req.body;
        const client = await MongoClient.connect("mongoDB URL")
        const db = client.db();
        const postItCollection = db.collection("postIts");
        const result = await postItCollection.updateOne({_id: data.id}, {$set:{positionX:data.y, positionY:data.x}});                                // <= if i not use ObjectId
        console.log(result);
        client.close();
        res.status(201).json({message:"success"})
    }
}
export default handler;

PS.我怎么能马上看到图片而不是网址呢?我看了其他的答案,做了同样的事情,但只有网址出来了...

dhxwm5r4

dhxwm5r41#

哦,我找到答案了...!这是new操作...
How do I search for an object by its ObjectId in the mongo console?

const result = await postItCollection.updateOne({_id: new ObjectId(data.id)}, {$set:{positionX:data.y, positionY:data.x}});

相关问题