简体   繁体   中英

Pass parameters to Firebase cloud function

I want to update the document fields using the parameters passed to cloud function. I am getting an error as 5 NOT_FOUND: No document to update: projects/react-ycce/databases/(default)/documents/papers/etc

values are passed in function, but I don't know how to put them in the query. Please help me.

exports.upload = functions.https.onCall((data, context) => {

    let dept = data.dept;

    let paperRef = db.collection("papers/"+data.dept);
    console.log(paperRef);

    return paperRef.update({
        name : data.doc_name,
        dept : data.dept,
        sem : data.sem,
        course_code : data.cc,
        type : data.resources,
        url: data.resourceLink,   
    })
    .then( () =>{ return("Successfully Uploaded")}
    )
    .catch(error => {
        console.log(error.message);
        return("Error while uploading");   
    });

});

You're building your reference with:

let paperRef = db.collection("papers/"+data.dept);

But db.collection returns a reference to a collection, while you're passing it the path to a document. So you should use:

let paperRef = db.doc("papers/"+data.dept);

If you're still getting an error after this, your data.dept probably doesn't contain the value you expect. I recommend adding a console.log(data.dept) right at the start of the query, and checking in the logging output what it prints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM