我目前正在尝试将文档插入到我的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
答案 0 :(得分:2)
TypeError:db.collection(...)。insert不是db.collection.find.execute.then.result(FacilitatorJS.js:26)at at的函数。
stitch-sdk JavaScript上的集合没有名为insert()
的函数。您应该使用insertOne()
代替。如果您尝试使用insertOne()
,则错误行可能会有所不同。
我建议您按照其中一个MongoDB Tutorials: Stitch开始使用。