0

In Vs Code there is a play icon on the top right of the window that when clicked runs the code in the opened file. I have a development server opened on my laptop.

Can I query my Db with the run code command?

For example, I have a file with code as seen below(I am using nodejs and mongodb via mongoose):

async function getUsers(){
    try{
        let Users = await User.find({})
        console.log(Users)
    } 
    catch(err){
        console.log(err)
    }
}

getUsers()

Everytime I run the VsCode run code command I do not see any any results or error logged to the console. How can I see them? Is it possible to query the DB this way? If not, how can I test my DB queries without involving the front end?

------------EDIT------------

  1. All I see in the console is : [Done] exited with code=0 in 0.583 seconds

  2. After trying again and again I am now getting the error SyntaxError: await is only valid in async function

  3. After removing the await I am now getting the mongodb query object returned to me rather than the results.

  4. After returning the await key word and changing my code to look like below now I am getting Promise { <pending> } as the result.

    async function getUsers(){
        try{
            let U = await User.find({})
            return U;
        } 
        catch(err){
            console.log(err)
        }
    }
    
    console.log(getUsers())
    

How can I resolve the promise and see the results?

YulePale
  • 6,688
  • 16
  • 46
  • 95
  • Are you talking about the builtin debugger or about some third-party extension like Code Runner? – Álvaro González Jun 20 '20 at 12:57
  • Not sure what you're trying to ask. Querying the database in the code? You are already doing that. Querying the database without front-end? You log to console which is already being done. This question needs elaboration – Quanta Jun 20 '20 at 12:58
  • I am not sure. It is a play button icon that has a tool tip saying "run code `Ctrl` + `Alt` + `N`" – YulePale Jun 20 '20 at 13:00
  • @ÁlvaroGonzález It is the [code runner extension](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner) – YulePale Jun 20 '20 at 13:02
  • @AbhijeetSoni I can not see the log in the console. That is what I am trying to figure out why. No errors or results. – YulePale Jun 20 '20 at 13:05
  • But... Does your code work to begin with? What you've shared is a function definition which you don't invoke. – Álvaro González Jun 20 '20 at 13:11
  • My code works I had just not put the full code in the question, sorry my mistake. I have updated the question. @ÁlvaroGonzález – YulePale Jun 20 '20 at 13:18
  • Do you save the file (`Ctrl` + `S`) after making changes? Code Runner ignores unsaved changes. – Álvaro González Jun 20 '20 at 13:36
  • Yes. I save. Does code runner runner work with async/await? – YulePale Jun 20 '20 at 13:48

1 Answers1

0

Use the MongoDB for VS Code extension.

This video explains how to set it up.

You can read more about it in the MongoDB docs.

YulePale
  • 6,688
  • 16
  • 46
  • 95