2017-10-31 16 views
0

업데이트 :비동기를 사용하는 올바른 방법, 약속에서 이중 다음()? node8, es5에 정말 바벨이 필요해?

console stringify에 대한 질문이 중복되었습니다.

내 질문이

const genericResolver = async (table, action , values) => { 

    resultValues = {}; 
    let resultValues = await models[table].findById(values.id).then(function (data) { 
     console.log(' ************** data'); 
     console.log(data); 
     return data; 
    }).then(function (data2) { 
     console.log(' ************** data 2'); 
     console.log(data2); 
     } 
    ); 
    console.log('------ resultValues'); 
    console.log(resultValues); 
    process.exit(1); 

for data and data2 I get: 

    tour { 
     dataValues: 
     { id: 'd61802ff-3eec-4a72-97ca-832f51b96bf0', 
     name: 'Chipre 2018', 
     price: '1400.00', 
     createdAt: 2017-09-05T04:01:27.642Z, 
     updatedAt: 2017-10-31T11:29:39.484Z }, 
     _previousDataValues: 
     { id: 'd61802ff-3eec-4a72-97ca-832f51b96bf0', 
     name: 'Chipre 2018', 
     price: '1400.00', 
     createdAt: 2017-09-05T04:01:27.642Z, 
     updatedAt: 2017-10-31T11:29:39.484Z }, 
     _changed: {}, 
     _modelOptions: 
     { timestamps: true, 
     validate: {}, 
     freezeTableName: true, 
     underscored: false, 
     underscoredAll: false, 
     paranoid: false, 
     rejectOnEmpty: false, 
     whereCollection: { id: 'd61802ff-3eec-4a72-97ca-832f51b96bf0' }, 
     schema: null, 
     schemaDelimiter: '', 
     defaultScope: {}, 
     scopes: [], 
     hooks: {}, 
     indexes: [], 
     name: { plural: 'tour', singular: 'tour' }, 
     omitNull: false, 
     sequelize: 
      Sequelize { 
      options: [Object], 
      config: [Object], 
      dialect: [Object], 
      queryInterface: [Object], 
      models: [Object], 
      modelManager: [Object], 
      connectionManager: [Object], 
      importCache: {}, 
      test: [Object] }, 
     uniqueKeys: {} }, 
     _options: 
     { isNewRecord: false, 
     _schema: null, 
     _schemaDelimiter: '', 
     raw: true, 
     attributes: 
      [ 'id', 
      'name', 
      'price', 
      'seatsmax', 
      'createdAt', 
      'updatedAt' ] }, 
     __eagerlyLoadedAssociations: [], 
     isNewRecord: false } 
왜, 내가 sequelize의 ORM 통해 서 기록을 얻을 비동기 기능을 가지고있는 샘 ecode

때문에 그래서 난, 비동기와 다른 내 질문을 변경했습니다

하지만 'resultValues'내가 얻을에 대한 :

undefined 

내가 바로 비동기로하고있어? 나는 node8을 사용하고 있습니다. 그래서 이론 상으로는 바벨 변환을 ES5로 변환 할 필요가 없습니까? 그냥 가져 오기 또는 내보내기? 나는 바벨 내 package.json

여기 ES6 코드를 작성하는 의심

개체, JS 참조로 전달됩니다 당신이 console.log(o)을 할 때, 당신이 콘솔에 실제 값을 볼 수 있기 때문입니다
{ 
    "name": "myproj", 
    "version": "1.0.0", 
    "description": "GraphQL server", 
    "main": "server.js", 
    "scripts": { 
    "start": "nodemon ./server.js --exec babel-node -e js", 
    "test": "eslint . --ext .js --ext .jsx --ignore-path .gitignore --cache" 
    }, 
    "author": "", 
    "license": "MIT", 
    "devDependencies": { 
    "babel-cli": "^6.24.0", 
    "babel-eslint": "^8.0.0", 
    "babel-preset-es2015": "^6.24.0", 
    "babel-preset-stage-0": "^6.22.0", 
    "eslint": "^4.7.1", 
    "eslint-plugin-react": "^7.3.0", 
    "nodemon": "^1.11.0" 
    }, 
    "dependencies": { 
    "bcrypt-nodejs": "0.0.3", 
    "body-parser": "^1.17.1", 
    "cors": "^2.8.3", 
    "dotenv": "^4.0.0", 
    "express": "^4.15.2", 
    "graphql": "^0.9.1", 
    "graphql-server-express": "^0.6.0", 
    "graphql-tools": "^0.10.1", 
    "jsonwebtoken": "^7.2.1", 
    "lodash": "^4.17.4", 
    "passport": "^0.4.0", 
    "passport-jwt": "^3.0.0", 
    "pg": "^7.2.0", 
    "sequelize": "", 
    "validator": "^6.2.0" 
    } 
} 
+0

가능한 중복을 [어떻게 POJO JSON.stringify 마법 작업을 계속 하시겠습니까?] (https://stackoverflow.com/questions/41747844/how-does-the-sequelize-pojo-json-stringify-magic-work) – Asthmatic

답변

2

(console.log을 호출 할 때 전달한 값이 아닙니다.

JSON.stringify을 사용하면 값으로 전달되는 문자열이 기록됩니다. 따라서 기본적으로 JSON을 사용하면 console.log을 호출 할 때까지 값이 표시됩니다.

편집 새로운 질문으로

, 문제는 두 번째 then 콜백에서 data2를 반환하지 않는다는 것입니다.

const data = await models[table].findById(values.id); 

을 또는 당신이 원한다면, 단지 data2를 반환하고 resultValues에서 같은 결과가 나타납니다 : async/await를 사용하는 경우, 당신은 모든 then를 사용할 필요가 없습니다의

let resultValues = await models[table].findById(values.id).then(data => { 
    console.log(' ************** data'); 
    console.log(data); 
    return data; 
}).then(data2 => { 
    console.log(' ************** data 2'); 
    console.log(data2); 

    return data2; // <== return here 
}); 

console.log('------ resultValues'); 
console.log(resultValues); 
+0

실제는 console.log입니다 그렇다면)? 기술로 동일한 가치를 얻는 방법이 있습니까? – DDave

+0

stringfy의 값이 어디에서 왔는지 이해할 수 없습니까? 내가 생각한 그 값을 변경하지 않았습니다. – DDave

+0

아니요, JSON을 사용할 때만 실제 값이 보존됩니다. 문제는'console.log'와는 다르지만 코드가 있습니다. 아직 할당되지 않은 시점에 몇 가지 속성이있을 것으로 예상됩니다. 나중에 파일을 할당 할 것입니다 (사용중인 파일과 다른 위치에서 발생할 수 있습니다). –