2

我目前正在尝试向我的 mongodb 插入一个文档,但是当我在集合上调用 insert() 时,会产生一个错误说 (FacilitatorJS.js:26 Uncaught (in promise) TypeError: db.collection(...).insert不是 db.collection.find.execute.then.result (FacilitatorJS.js:26) at 的函数

有人可以帮忙吗?我试过 insertOne() 也没有运气。

这是下面的代码

//prep server connection
    const clientPromise = stitch.StitchClientFactory.create('CODE');
    var client;
    var db;
    let stitchClient;
    /**
     * Connects to DB and can retrieve a facilitator's existing id, or create a new one (if name doesnt exist in db)
     * @param {dict} name //name to find or insert
     */
    function connect(name){
        clientPromise.then(stitchClient =>{
            client = stitchClient;
            db = client.service('mongodb', 'mongodb-atlas').db('budgetopolis');

            return client.login().then(getFacilitatorID(name))
        });
    }
    function getFacilitatorID(name){
        db.collection('Facilitators').find(name).execute().then(result => {
            if(result.length == 0){
                //create a facilitator object in db if doesn't exist
                db.collection("Facilitators").insert(name).execute().then(result1 => {
                    //now get the newly added Facilitator's ID
                    console.log('created new user')
                    db.collection('Facilitators').find(name).execute().then(result2 => {
                        var fullId = result[0]['_id']
                        Facilitator.session_id = fullId.toString().slice(-4)
                        updateMessageBar("Your new session id is: " + Facilitator.session_id)
                    });
                });
            }else{ //working
4

1 回答 1

2

TypeError: db.collection(...).insert is not a function at db.collection.find.execute.then.result (FacilitatorJS.js:26) at )

在stitch-sdk JavaScript上没有调用insert()集合的函数。你应该改用。如果您尝试过,错误行可能会有所不同。insertOne()insertOne()

我建议遵循MongoDB 教程之一:Stitch开始。

于 2018-03-06T01:56:11.910 回答