0

Hi everyone I'm beginner in Nodejs and mongoose.I have tried to insert and retrieve the data in mongoose.I'm using async await function to execute one by one (sequence).can anyone help me? Thanks in advance....

i.e: I want to execute (Async await)concept (SEQUENCE STEP)

1.connect the db

2.create the user

3.find the user.

I'm getting the error :

async function calltaskone(){
      ^^^^^^^^
SyntaxError: Unexpected token function
    at Object.exports.runInThisContext (vm.js:78:16)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3
    
    
    Code for your reference:
    
    
    'use strict';

const mongoose=require('mongoose');


const calldbconnect=()=>{
    return new Promise((resolve,reject)=>{
        if(true){
            mongoose.connect('mongodb://vdsd:vdwdwh12dw3,@ds11dwdw.mlab.com:1w5664/vorganisation',{useNewUrlParser:true},(err,db)=>{
                if(err){
                    console.log(err);
                    reject('Db is not connected');
                }
                else{
                    resolve('Db is connected');
                }
            });
        }
    });
}

const schemadesign=new mongoose.Schema({
    clientName:String,
    clientId:Number,
    clientAddress:String
});


const modeldata=mongoose.model('clientInfo',schemadesign);

const data=[{
    clientName:'VIGNESH Mack',
    clientId:4128,
    clientAddress:'UK'
},{
    clientName:'VIGNESH Tokyo',
    clientId:4988,
    clientAddress:'USA'
}];

function calldatasave(){

    return new Promise((resolve,reject)=>{

        modeldata.create(data,(err,a,b)=>{
            if(err){
                reject(`Error occured while data saved ${err}`);
            }
            else{
                resolve('Data saved successfully');
            }
        });
           
    });
}

const calldatafind=()=>{

    return new Promise((resolve,reject)=>{

        if(true){
            console.log('try to find');
            modeldata.find({'clientId':4988},(err,data)=>{
                if(err){
                    reject(`Error occured while find data: ${err}`)
                }
                else{
                    console.log(data);
                    resolve('Data found');
                }
            });
        }  
    });
}

async function calltaskone(){

    const a=await calldbconnect();
    console.log(a);
    const b=await calldatasave();
    console.log(b);
    const c=await calldatafind();
    console.log(c);

}

calltaskone();
demo developers
  • 161
  • 4
  • 18

1 Answers1

4

I believe you're using a older version of Node. Async functions are not supported by Node versions older than version 7.6. You can check here.

If you want to use async/await then you need to transpile using Babel for your node version.

Edit:

As you said you are using v7.3, you can use (from v7.0 to v7.5) the --harmony flag to enable the experimental features. To know more about the flag, check this out: What does `node --harmony` do?

Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35