插入不适用于Mongo Stitch(JS)

问题描述 投票:2回答:1

我目前正在尝试将文档插入到我的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
javascript mongodb mongodb-stitch
1个回答
2
投票

TypeError:db.collection(...)。insert不是db.collection.find.execute.then.result(FacilitatorJS.js:26)at at的函数

insert()上没有名为stitch-sdk JavaScript的函数。你应该使用insertOne()代替。如果您尝试使用insertOne(),则错误行可能会有所不同。

我建议跟随其中一个MongoDB Tutorials: Stitch开始。

© www.soinside.com 2019 - 2024. All rights reserved.