Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
621 views
in Technique[技术] by (71.8m points)

node.js - UnhandledPromiseRejectionWarning: Unhandled promise rejection. When using Pusher for real time mongoDb

I'm trying to make my mongodb realtime using pusher in my node-express app and react frontend app. After googling forours I couldn't find anything that solved my problem but I did get to know some things like when I don't attach an image in my Post when submitting my form, it listens and updates correctly but when I do attach an image in my Post then it gives my server error:

(node:1344) UnhandledPromiseRejectionWarning: Error
    at new RequestError (E:Web DevelopmentSimple Social Media App using MERN stackackend
ode_modulespusherliberrors.js:13:16)
    at E:Web DevelopmentSimple Social Media App using MERN stackackend
ode_modulespusherlib
equests.js:61:17
    at processTicksAndRejections (internal/process/task_queues.js:93:5)      
(node:1344) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a 
catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 4)

This is my node-express code to listen to changes:

mongoose.connection.once("open", () => {
    console.log("DB Connected !");
    
    const changeStream = mongoose.connection.collection("users").watch();
    
    changeStream.on("change", (change) => {
        
        if(change.operationType === "insert"){
            console.log("Triggering pusher **POST CREATED**");
            const postDetails = change.fullDocument;
            pusher.trigger("users", "inserted", {
                author: postDetails.author,
                message: postDetails.message,
                imageFile: postDetails.imageFile
            })
        } else if(change.operationType === "delete"){
            console.log("Triggering pusher **POST DELETED**");
            pusher.trigger("users", "deleted")
        }   
        else {
            console.log("Unknown trigger from pusher !");
        }
    })
})

This is my react app code:

const getData = async () => {
        const allPosts = await getAllPosts();
        setPosts(allPosts);
    }
useEffect(() => {
        const pusher = new Pusher('this is my id', {
            cluster: 'ap2'
          });
          const channel = pusher.subscribe("users");
          channel.bind('inserted', () => {
            getData();
          });
    }, [])

These are my middlewares:

app.use(express.json({limit:'50mb'}));
app.use(cors());

As said earlier, the problem only occurs when there is an image in my Post (I think maybe because of image size), otherwise if i do not include image, it will happily fetch newly posted data from server and refresh without reloading the whole app !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...